Home » Swift » Navigation bar disappearing when pushed programmatically

Navigation bar disappearing when pushed programmatically

Posted by: admin January 4, 2018 Leave a comment

Questions:

I’ve been working on an app for a while now and it pushes a view controller from the AppDelegate. When it does the navigation bar that was previously on the view controller being pushed disappears. Right now it’s a static nav bar (not part of a navigation controller), but it has been previously and still didn’t work. What am I doing wrong? Is there some sort of workaround?

This is the code in the AppDelegate that I’m using to push it:

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var PostView: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("NewView")

var rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(PostView as! UIViewController, animated: true)

pushViewController is the one I’m trying to push.

Answers:

On the view controller that you are trying to push, on the viewDidLoad method, try to force the navigation bar to not be hidden

self.navigationController?.setNavigationBarHidden(false, animated: false)

If this helps, check the view controller on the storyboard if you’re not forcing the view controller to hides the navigation bar

Hope that helps 🙂

Questions:
Answers:

The rootViewController is a UINavigationController. What ever view controller you push against this will use the UINavigationBar defined in that UINavigationController.

If you do not want the “automated” UINavigationBar that comes with UINavigationController, just must set some other type of view controller as the initial one (or set no view controller as initial).

Questions:
Answers:

Try this:

var storyboard = UIStoryboard(name: "NewStoryBoard", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "NewView") as! UIViewController

self.navigationController?.pushViewController(vc, animated: true)