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

cocoa-touchiosuinavigationcontrolleruitabbarcontroller

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?

Best Answer

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.

Related Topic