Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set navigation bar hidden, depending on how the view controller appeared

I have a tab bar with a navigation controller in one of the tabs. Currently the root view of the navigation controller doesnt have the nav bar showing and animates nicely into the subviews by

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

and

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

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

But of course changing tabs initiates the viewWillAppear function and so as I go back to the root view the navigation bar slides away, rather than just not being there.

Is there a way that I can hide the nav bar on the root view without animating it except for when appearing from a subview on the navigation stack?

like image 401
Josh Avatar asked Aug 18 '11 12:08

Josh


2 Answers

The (BOOL)animated parameter on viewWillAppear:animated. When changing Tabs, it will come as NO, since the animation is immediate. On the other hand, if it's being pushed or popped from the navigation stack with animated:YES, then it will come as YES.

Although this looks like a hack, it's the correct way: you don't need to figure out who was the caller, instead, focus on the fact that if your view controller will appear animated, you have time to do your own animations, if not, screw it, show (or in this case, hide) everything immediately.

like image 147
Can Avatar answered Oct 10 '22 04:10

Can


Try showing/hiding the bar in the UINavigationController's delegate's navigationController:willShowViewController:animated: method, depending on whether the view controller being shown is your root view controller.

like image 23
Anomie Avatar answered Oct 10 '22 06:10

Anomie