328

How can I hide a navigation bar from first ViewController or a particular ViewController in swift?

I used the following code in viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = true
}

and also on viewWillAppear:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

Both methods hide the navigation controller from all ViewControllers.

Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27
Vettiyanakan
  • 7,957
  • 6
  • 37
  • 55

14 Answers14

512

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)
}
Rengers
  • 14,911
  • 1
  • 36
  • 54
  • 3
    This answer is more efficient . Think of the repetitive code with each new ViewController you add. http://stackoverflow.com/a/39679506/5079380 – Amr Eladawy Jan 09 '17 at 08:18
268

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)
}
Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
Michael Garito
  • 3,005
  • 1
  • 11
  • 11
80

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)
}
Deepesh
  • 8,065
  • 3
  • 28
  • 45
27

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)
    }
Community
  • 1
  • 1
Ankahathara
  • 2,866
  • 2
  • 20
  • 26
8

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;
}
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
Dilip Jangid
  • 754
  • 1
  • 10
  • 20
6

Ways to hide Navigation Bar in Swift:

self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
5

Want to Hide the NavigationBar on the First ViewController

override func viewWillAppear(animated: Bool) {
   self.navigationController?.isNavigationBarHidden = true
}

Want to Show the NavigationBar on the Second ViewController

override func viewWillAppear(animated: Bool) {
   self.navigationController?.isNavigationBarHidden = false
}
Chithian
  • 253
  • 3
  • 8
3

Ways to show Navigation Bar in Swift:

self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.isNavigationBarHidden = false
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
3
     private func setupView() {
            view.backgroundColor = .white
            navigationController?.setNavigationBarHidden(true, animated: false)
        }

Alternative

in viewDidLoad use this settings

title = "Madman"
navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

Check the constraints of Collectionview, scrollview or tableView

 NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
Wasim
  • 921
  • 12
  • 14
1
    /*.  Swift 5  */     
    let controller =  self.storyboard?.instantiateViewController(withIdentifier: "sc_userNavigation") as! UserNavigationViewController
    let navigationController = UINavigationController(rootViewController: controller)
    navigationController.setNavigationBarHidden(true, animated: false)
    navigationController.modalPresentationStyle = .fullScreen
    self.present(navigationController, animated: false, completion: nil)
Mirza Q Ali
  • 477
  • 5
  • 6
0

In IOS 8 do it like

navigationController?.hidesBarsOnTap = true

but only when it's part of a UINavigationController

make it false when you want it back

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

I use a variant of the above, and isolate sections of my app to be embedded in differing NavControllers. This way, i don't have to reset visibility. Very useful in startup sequences, for example.

drew..
  • 3,234
  • 3
  • 16
  • 19
0

Call the set hide method in view Will appear and Disappear. if you will not call the method in view will disappear with status false.It will hide the navigation bar in complete navigation hierarchy

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

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated:true)
}
Talha Rasool
  • 1,126
  • 14
  • 12
  • This is a copy/paste response. What is the difference between your response and the other 2 or 3 equal responses here?? – Starsky Apr 13 '20 at 13:45
-3

You can do it from the window controller (Swift3)

class WindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        window?.titleVisibility = .hidden
    }
}
Pedro Luz
  • 2,694
  • 4
  • 44
  • 54