-->

How to hide/disable only the first uinavigationbar

2019-03-25 06:29发布

问题:

I've been wandering how to hide / remove / disable only the main or first navigation bar in the navigation controller so that I could put an image as a whole background screen but I couldn't find any solution.

Did try hide the titleview in viewdidLoad of the main navigation controller but didn't work. Tried using navigationBarHidden but it hides the whole navigation bar for the next stack of controller.

So, I'm not sure how to do this. To give you an example, I would like to have something like this app - The Masters Golf Tournament - http://appshopper.com/sports/the-masters-golf-tournament.

If you look at Screen 1, it doesn't have any nav bar at the top but when you touch any options it will push to a new view controller and have the nav bar appear as in Screen 3,4 and 5.

Hope anyone could help me with this.Thanks a lot!

回答1:

In most of my applications I have a custom UIViewController class that I derive all other custom controllers from. In some of these, I added a method like navigationBarInitiallyHidden to the base class that other classes can override. The default result depends on the nature of the application.

In the delegate of the navigation controller, when a controller is being shown that implements that method, the delegate hides or shows the navigation controller accordingly. Since I animate the hide or show, I check the current state and do nothing if no change is needed.

You could do something simpler in your delegate method. If the controller being shown is the root controller, hide the navigation bar, otherwise show it if it is hidden.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  if ( viewController == rootController ) {
    [navigationController setNavigationBarHidden:YES animated:animated];
  } else if ( [navigationController isNavigationBarHidden] ) {
    [navigationController setNavigationBarHidden:NO animated:animated];
  }
}


回答2:

You can hide the navigation bar: [self.navigationController setNavigationBarHidden:YES]; and where you want to show the navigation bar again [self.navigationController setNavigationBarHidden:NO];



回答3:

hide/disable

self.navigationController.navigationBarHidden = YES;

show/Enable

self.navigationController.navigationBarHidden = NO;


回答4:

You can hide navigation bar by using bellow code. Below code will hide navigationbar at the time of viewWillAppear.

Objective C

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

Swift

self.navigationController?.setNavigationBarHidden(true, animated: animated)