ios - How to hide a navigation bar from first ViewController in Swift?

ID : 10091

viewed : 20

Tags : iosswiftuinavigationcontrolleruinavigationbarios

Top 5 Answer for ios - How to hide a navigation bar from first ViewController in Swift?

vote vote

96

If you know that all other views should have the bar visible, you could use viewWillDisappear to set it to visible again.

In Swift:

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

82

Swift 3

override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)      // Hide the navigation bar on the this view controller     self.navigationController?.setNavigationBarHidden(true, animated: animated) }  override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)      // Show the navigation bar on other view controllers     self.navigationController?.setNavigationBarHidden(false, animated: animated) } 
vote vote

71

You can unhide navigationController in viewWillDisappear

override func viewWillDisappear(animated: Bool) {     super.viewWillDisappear(animated)     self.navigationController?.isNavigationBarHidden = false } 

Swift 3

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

70

You could also create an extension for this so you will be able to reuse the extension without implementing this again and again in every view controller.

import UIKit  extension UIViewController {     func hideNavigationBar(animated: Bool){         // Hide the navigation bar on the this view controller         self.navigationController?.setNavigationBarHidden(true, animated: animated)      }      func showNavigationBar(animated: Bool) {         // Show the navigation bar on other view controllers         self.navigationController?.setNavigationBarHidden(false, animated: animated)     }  } 

So you can use the extension methods as below

    override func viewWillAppear(_ animated: Bool) {         super.viewWillAppear(animated)         hideNavigationBar(animated: animated)     }      override func viewWillDisappear(_ animated: Bool) {         super.viewWillDisappear(animated)         showNavigationBar(animated: animated)     } 
vote vote

56

In Swift 3, you can use isNavigationBarHidden Property also to show or hide navigation bar

override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)     // Hide the navigation bar for current view controller     self.navigationController?.isNavigationBarHidden = true; }  override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)     // Show the navigation bar on other view controllers    self.navigationController?.isNavigationBarHidden = false; } 

Top 3 video Explaining ios - How to hide a navigation bar from first ViewController in Swift?

Related QUESTION?