Swift How to present Tabbar on Button click - ios

In my project i want to present Tabbar on button click, now i have already created tabbar and i give identity name as "tabbar" that i show you in below image
so now i am using below code to call Tab bar controller but i am not getting it.
let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
navigationController?.pushViewController(tabbar, animated: true)
can you guys suggest me what i need to do and which code is useful to me to present Tabbar controller.
For more specification : i added below image in which there is blue button in one viewController i want to present tab bar controller on click of that blue button
Thank you.

Try this and see:
Using Storyboard Segue: Connect your segue as present modally with your action button.
Programatically: Use self of view controller (UIViewController) and present it modally, like this.
if let tabbar = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController) {
self.present(tabbar, animated: true, completion: nil)
}
Here is result:

Use this, you dont have the Navigation controller over there, thats why it won't push that way, instead, you need to use following code:
self.present(tabbar, animated: true, completion: nil)

You should be aware that Apple's HIG (Human Interface Guidelines) say that if you have a tabbed application that should be the root-level navigation for the entire app. you're not supposed to do what you are trying to do from a human interface perspective.
That said, it should be technically possible.
My guess is that you don't have a navigation controller.
Use the debugger to check the value of self.navigationController, or add a print statement:
let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
print("navigationController = \(navigationController)")
print("tabbar = \(tabbar)")
navigationController?.pushViewController(tabbar, animated: true)
If either navigationController or tabbar displays as nil, that's your problem.

Select the viewController from which the button click triggered and Select Editor form xcode menu->Embed In->NavigationController.
then write this in button action
let tabbar: UITabBarController? = (self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
self.navigationController?.pushViewController(tabbar!, animated: true)

According to your images I think that your tabBarController has no back button. Means user cannot go back.
If that is the case you can do this.
let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "tabBar")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBar

Related

How to Tabbar Show in Another Sidemenu item?

Why my Tabbar Show in Another Sidemenu item, here is my code
let navigate = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
navigationController?.pushViewController(navigate, animated: true)
For achieving this you have to add navigation controller before making root view controller from tabbar like shown in image. Then it will shown to your another controller.

Navigation bar not appearing

Can someone help me with this? :(
The navigation bar is not appearing. This is presented by a rootViewController.
You have not posted any code still guessing.
Just taking UINavigationController in storyboad doesn't means that it will appear by default.
You are just presenting view controller. You need to embed in navigation controller or give storyboard identifier to navigation controller and present that.
Example :
let yourviewController = self.storyboard?.instantiateViewController(withIdentifier: "yourVCIdentifer")
let nav = UINavigationController(rootViewController: yourviewController)
self.present(nav, animated: true, completion: nil)

Top navigation bar doesn't appear

I have a top navigation bar that has a logout button which returns the user back to the login screen and wipes their access token from their keychain.
I am working on adding a slide out menu bar, however, my top navigation bar is not appearing and I can't seem to get it to appear.
I am presenting this view on successful login using the following excerpt of code:
DispatchQueue.main.async {
let homePage =
self.storyboard?.instantiateViewController(withIdentifier:
"HomePageViewController") as! HomePageViewController
self.present(homePage, animated: true)
}
Instead of present HomePageViewController, you have to present UINavigationController of HomePageViewController.
Set storyboard ID for UINavigationController of HomePageViewController. For example, you set storyboard ID for UINavigationController is HomePageNavigation
Replace your code with the code below.
DispatchQueue.main.async {
let homePage =
self.storyboard?.instantiateViewController(withIdentifier:
"HomePageNavigation") as! UINavigationController
self.present(homePage, animated: true)
}
Select UINavigationController of HomePageController on Storyboard
Change Storyboard Id Of UINavigationController.

How to have TabBarController at the bottom itself on selecting menu Item from the side menu?

I'm using SWRevealController to have a side menu. In my app ,it also have UITabBarcontroller.
My connection format is as SWRevealViewController--->UItabbarController--->NavigationController--->UITabbaritemPage-->Another vc
PLEASE CLICK ON THE IMAGE TO SEE IN CORRECT ORIENTATION
The above show is the layout I'm using.I want to have that burger button (menu button) in almost all vc that are showing from and in tabbarcontroller. Currently I'm getting the side menu when tapping on the Button (The image showed in right side as spereate).On choosing a menu, it shows the desired vc but,the bottom tab bar is not there. I want to have the bottom tabbar in entire pages also in pages from the side menu.
How can I acheive this? Please help me.
The code I'm using in didSelectRowAtIndexPath is:
if indexPath.row == 1{
let destinationVc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
let newFrontVc = UINavigationController.init(rootViewController:destinationVc!)
revealViewController.pushFrontViewController(newFrontVc, animated: true)
}
I think you don't actually need to push a view controller if using tab bar controller.
let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController")
tabBarController.selectedIndex = 1
revealViewController.pushFrontViewController(tabBarController, animated: true)
You would have to set the identifier of the tab bar controller to be TabBarController in Main.storyboard for this to work.
I also had the same layout like you only few view controllers has been added to the tabbar and it will show the tab bar in all view controllers
SWRevealViewController with TabBarController using XIB in Swift 4
let objSideBarVC = SideBarVC(nibName: "SideBarVC", bundle: nil)
let navSidebar = UINavigationController(rootViewController: objSideBarVC)
navSidebar.navigationBar.isHidden = true
let objDashboardVC = DashboardVC(nibName: "DashboardVC", bundle: nil)
let navDashboard = UINavigationController(rootViewController: objDashboardVC)
navDashboard.navigationBar.isHidden = true
let mainRevealController = SWRevealViewController.init(rearViewController: navSidebar,frontViewController: navDashboard)
AppDelegate().window?.rootViewController = mainRevealController
mainRevealController.pushFrontViewController(TabBarController, animated: true)

Swift ios send user to a specific ViewController in tab based application

I want to send a user to a specific ViewController in my app once a notification is clicked.
I now that I can do something like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)
But my app has a tab bar and looks like this
Tab bar
tab1: navigationController -> VC1
tab2: navigationController -> VC2 -> HomeVC
tab: navigationController -> VC3
Each tab has a navigationController as a infront of it.
So how can I send the user to HomeVC? I must first select tab 2 then the navigation controller then push the user tvice:
tab2: navigationController -> VC2 -> HomeVC
And the other problem, if there any way to tell if the user is already in HomeVC? I dont want to send the user to the same VC if his already there.
You must have access to your UITabbarController in you UIApplicationDelegate or wherever you're handling the notification tap.
let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController //or whatever your way of getting reference is
So first you'll get the reference to UINavigationController in your second tab like this:
let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController
Now push your home view at second tab's navigation controller:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
navInTab.pushViewController(destinationViewController!, animated: true)
And finally switch your tab to second to show the just pushed home controller
tabBar.selectedIndex = 1
Keep in mind, this answer assumes that your application has already set the tab bar as the root view controller of application window prior handling the notification tap.
Try something like this:
if let tabBarController = window?.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1 // in your case the second tab
}
The idea is to switch to get the tab bar instance and switch it to your desired tab (where you have your view controller).
The above code works in AppDelegate / you can easily call it anywhere by getting the tabBarController instance.
You can check which tab is selected by user with the method var selectedIndex: Int. You can check which view controller is present like this self.navigationController?.presentingViewController?.presentedViewController. This will solve your problem.

Resources