heyred
heyred

Reputation: 2051

Unable to hide navigation bar in xcode

I have made a simple app using the Storyboard editor in xcode 4.6.3. The first view is a navigation controller with some simple buttons for navigation. This then by default adds the navigation bar to the top of each new view I create when I connect the buttons to each of their pages.

However, I want the first page (landing page I guess I would call it) to have no top bar. I follow the instructions here on how to disable the top navigation bar in Storyboard mode. However, this then disables all navigation bars for all views linked to this main view.

I also change the colour of sub pages' top navigation bars but this does not work either. I run the application on the emulator but the changes dont seem to take affect.

Can anyone please advise? I am new to objective c (experience in Java mostly) and would like to get an app out quickly. My problem is time and Storyboard seems to have solved this as I can get something together fairly quickly.

Upvotes: 5

Views: 23393

Answers (4)

Radu Ursache
Radu Ursache

Reputation: 1481

in Swift you can use the almost obvious

self.navigationController?.navigationBar.isHidden = true

and

self.navigationController?.navigationBar.isHidden = false

to show or hide the navigation bar. make sure you allow the view to load so call those in viewWillAppear or viewDidAppear.

Upvotes: 3

Ernest
Ernest

Reputation: 79

The answer below is correct and is relevant for Swift 4. Another posts use viewWillDisappear to show the navigation bar again instead of viewDidDisappear, but this does not work. For everyone with this problem in swift 4 DO NOT USE:

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

To show the navigation bar again use:

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

Upvotes: 0

Dan Korkelia
Dan Korkelia

Reputation: 104

This might be an old post but seems still relevant. I ran into this issue and thought this might be useful to update to Swift Version 4.

Swift 4

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

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

For example, you would insert it into initial view controller where you don't want to see navigation bar. This will allow it to hide navigation bar. And animate it into view on next segue.

One thing to keep in mind, because it animates into view you should make sure your constraints are not aligned to save area which includes navigation bar but rather to superview.

Hoping this is helpful.

Upvotes: 0

Adam21e
Adam21e

Reputation: 791

I just fired up an app and had the same issue, the line you are looking for is:

self.navigationController.navigationBar.hidden = YES;

Full Code is:

- (void)viewWillAppear:(BOOL)animated
{
     self.navigationController.navigationBar.hidden = YES;
}

Make sure you turn it back on with the next controller:

self.navigationController.navigationBar.hidden = NO;

Was only tested in a later version of Xcode but should work fine for 4.6.3

(edit to change from viewDidLoad to viewWillAppear)

Upvotes: 21

Related Questions