I created View Controllers and I want to add Navigation Controller now. I tried embed-in Navigation Controller to My DetailViewContoller(colorful View Controller) but I got an error. main.storyboard, error. How can I add Navigation Controller ?
In order to set view controller as navigation controller, you can set it programmatically by calling this function in appdelegate or scenedelegate or any point.
func setNavigationController() {
var viewController: UIViewController
let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "myVCID")
let rootViewController = UINavigationController(rootViewController: viewController)
rootViewController.setNavigationBarHidden(true, animated: false)
self.view.window?.rootViewController = rootViewController
}
If you want to set navigation controller through storyboard, then you have to first add navigation controller and set is root view controller to the controller you want to make it navigation controller.
Related
I am trying to embed my view controller into a navigation view controller so i get the navigation bar and all the other stuff like back buttons. I want to do it programmatically.
It is done like this:
// example ViewController
let myVC = UIViewController()
// create the NavigationController with my VC as root
let navCon = UINavigationController(rootViewController: myVC)
A view controller is pushed and the bottom tab bar is hidden like so:
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
That works fine.
However when I change the root view controller before the push the bottom bar is not hidden.
// Change the root view controller
let firstRootViewController = UIApplication.shared.keyWindow!.rootViewController
UIApplication.shared.keyWindow!.rootViewController = secondRootViewController
// Push view on stack of navigation controller which is a child of firstRootViewController
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
// Some more things happen...
// Switch back to previous root view controller
UIApplication.shared.keyWindow!.rootViewController = firstRootViewController
The result is that the navigation controller pushed the myViewController correctly but the bottom bar is visible, as if the parameter hidesBottomBarWhenPushed was ignored.
What's wrong here?
The solution was to not change the root view controller but to only add the view to the keyWindow:
// Add another view on top of all views
UIApplication.shared.keyWindow?.addSubView(self.view)
// Push view on stack of navigation controller which is a child of firstRootViewController
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
// Some more things happen...
// Remove topmost view
self.view.removeFromSuperview()
I have two storyboard (Main and SB2)
I have one view controller in storyboard in Main and one in SB2
I perform
let SB2 = UIStoryboard(name: "SB2", bundle:nil)
let vc : UIViewController = self.SB2.instantiateViewControllerWithIdentifier("VC2")
self.showViewController(vc, sender: self)
After the second viewcontroller loads and this command is run, it prints nil:
print(self.navigationController) // prints nil
In SB2 (second storyboard) I clicked on the viewcontroller (VC2) and then clicked on Editor > Embed In > Navigation Controller. This placed a navigation controller with the storyboard and the root view controller is VC2. I triple checked this. The first sign of it being connected was the gray navigation bar on top. The second being that there is segue connecting the navigation controller to VC2 and the last place I could have checked is in the navigation controller utilities.
I am thinking that maybe I shouldn't transition from VC1 to VC2 directly but rather VC1 to NavigationController however I don't know how to do this or if its even possible.
I don't know when sb2 prints nil when in face a navigation controller is connected to it. Any help is appreciated.
You need to push the view controller (VC2) on to the navigation controller.
let SB2 = UIStoryboard(name: "SB2", bundle:nil)v
let nvc = SB2.instantiateViewControllerWithIdentifier("NVC") as! UINavigationController
let vc : UIViewController = self.SB2.instantiateViewControllerWithIdentifier("VC2")
nvc.pushViewController(vc, animated: true)
Then in your view controller try calling
self.navigationController
In your storyboard,
select the initial ViewController. With this view controller selected, choose the menu item
Editor -> Embed In -> Navigation Controller
I need to access the UITabBarController, and the second of its subviews from the appDelegate.
This is what I have tried in applicationDidEnterBackground:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let tabController = leftNavController.tabBarController! as UITabBarController
let controllers : Array = tabController.viewControllers!
print("viewcontrollers \(controllers)")
The app crashes, complaining that tabController is nil. If i remove the UINavigation controller from storyboard, the UITabBarController is accessed easily with:
let tabController = splitViewController.viewControllers.first as! UITabBarController
What is the correct way to access the childcontrollers of the UITabBarController, where a UISplitView is the root?
Finally I found a solution. I had to use "childViewControllers" of the navigation controller like this:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let tabController = leftNavController.childViewControllers.first as! UITabBarController
let viewControllers : Array = tabController.viewControllers!
print("viewControllers \(viewControllers)")
Now I can easily access any of the viewControllers and run their methods from appDelegate :-)
Rather than embedding your tab bar controller in a navigation controller, you should embed the child view controllers in their own navigation controllers, like this:
Split View -> Tab Bar -> Navigation Controller #1 -> View Controller
-> Navigation Controller #2 -> View Controller
This is the correct way to use a tab bar in conjunction with a navigation controller.
I have VC1 which is a basic view controller, I then want to present VC2 which is inside a navigation controller. Whenever I present it, it doesn't display the navigation controller. I want this all done pragmatically. The code I have been using to present VC2 is:
func matchesPressed(sender: UIButton!) {
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
self.presentViewController(matchesTVC, animated: true, completion: nil)
}
How do I present the navigation controller it is inside as well?
Is the navigation controller in the storyboard? If so, give it a storyboard ID and replace your matchesTVC stuff with the navigation controller.
If matches is a standalone view controller in the storyboard, you can do it in code like this:
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
let navContr = UINavigationController(rootViewController:matchesTVC)
self.presentViewController(navContr, animated: true, completion: nil)
Instantiate the navigation controller that contains your view controller and present the navigation controller.