Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the UINavigationBar for my first view

I am wanting to hide the UINavigationBar that automatically loads with the Navigation project for the first view only and am wondering how I can make this work.

I have tries to do it like this

//RootViewController.m

#import "mydelegate.h" //-- this is where the view controller is initialized

//...

- (void)viewDidLoad
{
    [super viewDidLoad];
    navigationController *navController = [[navigationController alloc] init];
    [navigationController setNavigationBarHidden:YES animated:animated];
}

//.....

However I am getting errors because I guess I am not calling navigationController across from the delegate file properly or this is just not possible to call it like you would a method from another class.

Any help would be greatly appreciated.

like image 678
C.Johns Avatar asked Dec 12 '22 10:12

C.Johns


1 Answers

There are a couple things wrong here.

  1. You should be accessing the navigation controller that's presenting your view controller by using self.navigationController. Your code snippet is creating a new UINavigationController; hiding that bar won't get you anything.
  2. Instead of hiding the navigation bar in viewDidLoad, you should hide it in viewWillAppear:. You can hide the navigation bar in viewDidLoad, and the navigation bar will be hidden when the view is initially presented, but if you were to push another view that presented the navigation bar and hit the back button, the navigation bar would remain visible.

Your viewWillAppear: should look something like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

And the viewWillAppear: methods in other view controllers you push on this navigation controller should show or hide the navigation bar appropriately.

like image 61
Jose Ibanez Avatar answered Dec 15 '22 01:12

Jose Ibanez