How to Tabbar Show in Another Sidemenu item? - ios

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.

Related

What is different between this when we navigate one view controller to another

What is the difference between this first call:
let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
self.present(next, animated: true, completion: nil)
and this second:
let dashboard = self.storyboard?.instantiateViewController(withIdentifier: "DBVC") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)
The first usage will present the new view controller. This presentation normally slides the new controller up from the bottom. If you want to go back, you need to create a button or something similar to dismiss it.
The second usage will use the navigation controller to display (via push which normally slides in from the right) the new view controller. You will automatically get a "< Back" button in the navigation bar. But this will only work if the calling view controller is already embedded in a navigation controller, otherwise self.navigationController is nil.

Swift How to present Tabbar on Button click

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

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.

Swift can't fixed navigation bar for UITableView

I am trying to display a fix navigation bar for my UiTableViewController, I have a first ViewController and when I click on it, this will open my UITableViewController Here is the code of the click :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
presentViewController(vc, animated: true, completion: nil)
the UItableView is correctly display but not navigation bar appear, if I add one, the navigation bar scroll with the table view and I don't want this behavior.
I tried this without success :
Go to the Editor menu, and click on the Embed In submenu, and choose
Navigation Controller
And tried to change some settings here :
Actually, in your case you want to show navigation and for navigation you have to push your view controller to a UINavigationController thats why the solution is :
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)
presentViewController offers a mechanism to display a modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a parent controller & establish a parent child relation b/w presenting & presented view controllers.
where as
pushViewController offers a much more flexible navigation process where you can push & pop a new controller to UINavigationController, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right like building a stack of view controllers stacking upon each other.
Do it this way:
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)

Push To view controller from View is not working in swift

I tried to push to a view controller from a view class using tap gesture action. All the below lines are executed, but the control not pushing to respective view controller. I need to push to that view controller, but not to set the controller as root view,because i need to navigate back to some other navigated view controller. Any one please help me.
let webViewController = R2WebViewViewController()
currentWindow.rootViewController?.navigationController?.pushViewController(webViewController, animated: true)
try this
(currentWindow?.rootViewController as! UINavigationController).pushViewController(webViewController, animated: true)
Your root must be navVC
currentWindow.rootViewController?.navigationController?
Your problem is that your rootViewController is probably an UINavigationController already, so you should do:
let webViewController = R2WebViewViewController()
(currentWindow.rootViewController as? UINavigationController).pushViewController(webViewController, animated: true)

Resources