How to present navigation controller with tab bar controller - swift

I have navigation controller and tab bar controller. TBC is embeded in NC. When I present VC from another VC, I see only NC and not TBC there. I want them both to be presented. What should I do?
Here is my code:
let mainAreaVC = self.storyboard?.instantiateViewController(withIdentifier: "MainAreaVC") as! MainAreaVC
let mainAreaVCe = UINavigationController(rootViewController: mainAreaVC)
self.present(mainAreaVCe, animated: true, completion: nil)

If you want to show MainAreaVC with both NavigationController and TabBarcontroller then you need to present the UITabBarController instead of MainAreaVC. So in storyboard set the Storyboard Id for your TabBarController something like TabbarVC or what ever you want then use it wit instantiateViewController to get UITabBarController.
let tabbarVC = self.storyboard?.instantiateViewController(withIdentifier: "TabbarVC") as! UITabBarController
self.present(tabbarVC, animated: true, completion: nil)

Related

Go to a specific UIViewController

I have a RootViewController which is in a navigationController. The RootViewController adds a new navigationController in a view container, that has several UIControllers, the last one is PersonalInfoViewController.
From personalInfoViewController I want to go back to RootViewController. My RootviewController is not the view.window.rootViewcontroller
from any view controller of personalInfoViewController Go back
self.view.window.rootViewController?.dismiss(animated: true, completion: nil)
but you have to change Navigation like this image
let navController = UINavigationController(rootViewController: personalInfoViewController())
rootViewController.present(navController, animated: true, completion: nil)

How can I add a segue identifier to a programmatically modal transition?

I have a controller which is in the Main storyboard. When I click on a button, I call the displayBorneDetailsAction() action, it presents a modal view from another storyboard.
I would add a Segue identifier when I present my modal to pass data from my main view controller to my modal view controller (with prepareForSegue), but I don't know how to do it.
I tried to use performSegue(withIdentifier:), but it doesn't present the modal in the same way.
#IBAction func displayBorneDetailsAction(_ sender: Any) {
// open the modal of a borne
let storyboard : UIStoryboard = UIStoryboard(name: "Borne", bundle: nil)
let vc: BorneVC = storyboard.instantiateViewController(withIdentifier: "BorneVC") as! BorneVC
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
navigationController.edgesForExtendedLayout = []
self.present(navigationController, animated: true, completion: nil)
}
You cannot add an identifier to a programmatical segue, since you are able to access the instance of the controller that is being presented. Simply do anything you want with the controller in the function where you present it.

How to programmatically show modal in a UINavigationController

In my app, I have a UINavigationController that i'm pushing and popping ViewControllers from. At some point, I want to show a VC modally (showing the previous controller "underneath"). I can get it to work by setting up a segue in a storyboard, however I'm in a spot where I need to do it programmatically, and I can't seem to find the right magic incantation to make it work.
I saw a couple of similar questions but they seemed to be showing the UINavigationController modally, not showing one of the VC's on the UINavigationController stack modally.
(I put up a test application here: https://github.com/SuperTango/ModalNavController, and that's where this code and images come from)
The "Manual" code does:
#IBAction func goToVC2Tapped(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
but it's not working (see the second transition in the gif below).
The segue that works is setup like this:
This gif is from the test app and shows how it works with the segue, but not manually.
Any ideas? Thanks!
To present modally you need to use:
present(destinationViewController, animated: true, completion: { })
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
destinationViewController.modalTransitionStyle = .crossDissolve
self.present(destinationViewController, animated: false, completion: nil)
I think this helps show your ViewController in a modal way of presenting. Write the above code under outlet actions or where you want to present your ViewController.

Open view controller programatically and dont see navigation bar. Swift 3

On my story board I have a collection view controller embedded in a navigation controller. Now from the cell of the collection view, I programaticly open the second view controller (called TwoPicsViewController), like this:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TwoPicsViewController") as! TwoPicsViewController
self.present(nextViewController, animated:true, completion:nil)
However when my TwoPicsViewController opens, I don't see the navigation bar at all. On the story board I connected the TwoPicsViewController to the first view controller with a segue show.
Am I missing something else?
Thanks
To push a new UIViewController to the navigation stack you should use:
self.navigationController?.pushViewController(nextViewController, animated: true)
Instead of:
self.present(nextViewController, animated:true, completion:nil)
You are not putting the new vc on the navigation stack, try this:
performSegue(withIdentifier: "Segue Name", sender: nil)
And on Storyboard select the Segue, attributes inspector, identifier = "Segue Name"

How to segue programmatically segue

I've just finished a Swift tutorial which does not use Interface Builder. It is all programmatic. Everything looks great but now I have to segue back to the storyboard, I am lost.
I'd like to segue from the "Login" button designed in the Audible tutorial to the next view controller I created in my storyboard called "DashboardVC".
Here is a link to the tutorial and source code. https://www.letsbuildthatapp.com/course_video?id=382
TIA
As is all from code, you don't have segue's; either you have to push a new controller using pushViewController from NavigationController or instantiate a new ViewController, in this case your 'DashboardVC'.
Like this
let viewController = DashboardVC()
viewController.view.backgroundColor = .blueColor() //example
navigationController?.pushViewController(viewController, animated: true)
Or just presenting the controller using this
let vc = DashboardVC()
present(vc, animated: true, completion: nil)
Using Storyboard, should be like this; the view controller identifier have to be set in the storyboard as well
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"DashboardVC") as! DashboardVC
self.present(viewController, animated: true)