Push Page With/Without Navigation Bar

Sometimes when using UINavigationController, you need to show/hide navigation bar on the new page.

To have the navigation bar be “instantly” visible (or not visible) when you push a new page, let’s set up UINavigationController with two UIViewControllers (firstViewController, secondViewController).

Add a button in the firstViewController which pushes the secondViewController. To have the navigation bar in the firstViewController hidden and secondViewController shown, you’re going to add logic in the viewWillAppear and viewWillDisappear functions in the firstViewController. In the source code of the firstViewController, override the aforementioned methods:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}

That is everything you need to show/hide navigation bar in a new page. Simply switching the boolean values in setNavigationBarHidden method of UINavigationController will produce a reverse effect: navigation bar shown in the firstViewController and hidden in the secondViewController. One thing to note if you’re going for the reverse effect is that you’ll need to manually provide a way to dismiss the secondViewController once it’s been pushed.