Storyboard - Hiding top bar of navigation controller programmatically

56,741

Solution 1

Finally solved it. You should always hide navigation bar only through viewController.

In my question above I instantiated a whole new navigationController which didn't point at the real navController on the screen. You can obtain the "real" one through the view controller like this:

[viewController.navigationController setNavigationBarHidden:YES animated:YES];

Solution 2

There are 2 ways to hide top bar of Navigation Controller:

1) Programatically

[self.navigationController setNavigationBarHidden:YES animated:YES];

2) Using Interface Builder

You can uncheck "Shows Navigation Bar" in property list of Navigation Controller.

enter image description here

Please make sure you have selected scene which contain Navigation Controller. Example image of scene.
enter image description here

Solution 3

in swift 4:

self.navigationController!.navigationBar.isHidden = true

something like this:

override func viewDidLoad() {
        super.viewDidLoad()
       self.navigationController!.navigationBar.isHidden = true
    }

Solution 4

I faced same problem, this worked for me

navController.navigationBar.hidden = YES;

Solution 5

The answers are correct. Just want to add that you probably want to put back the navigation bar when leaving the view as other views might use it and when you hide for one its hidden for all.

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

and before you leave the view:

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

`

Share:
56,741
animal_chin
Author by

animal_chin

Updated on April 21, 2020

Comments

  • animal_chin
    animal_chin about 4 years

    I'm using a storyboard and I'm trying to hide a top bar of my main navigation controller when a certain button is pressed (or function is called). I know I have to initialize an object referring to a navigation controller from a storyboard (using identifiers), but when I send the setNavigationBarHidden message to this newly created object nothing really happens on screen.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UINavigationController *navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"MyNavController"];
    
    [navController setNavigationBarHidden:YES animated:YES];
    

    Does anyone know what the problem is?