Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self.navigationController.navigationBar setHidden:NO not working when view controllers are swapped out

Tags:

ios

ios6

I'm experiencing some weird behavior with hiding and showing the UINavigationBar.

In my viewWillAppear method I'm calling this:

self.navigationController.navigationBar.hidden = YES;

and when the user presses a button I'm calling this:

self.navigationController.navigationBar.hidden = NO;

and then swapping out the current view controller using the viewControllers property of a custom UINavigationController.

This works fine, but if I try to show the navigationBar using the same line in the viewWillDisappear method, it doesn't work. The navigationBar is still hidden.

I'm able to show/hide the status bar in viewWillDisappear using the following method:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

but showing/hiding the UINavigationBar doesn't work!

EDIT: Also, self.navigationController.isNavigationBarHidden returns NO after I try to show the bar in viewWillDisappear but the bar is still hidden.

Any help would be greatly appreciated. Thank you!

like image 856
Mike Buss Avatar asked Dec 16 '22 09:12

Mike Buss


2 Answers

Because you’ve already swapped the current view controller out of the stack, self.navigationController is probably nil when viewWillDisappear is called.

like image 72
eager Avatar answered Dec 28 '22 08:12

eager


You're better off not messaging self.navigationController from viewWillDisappear as eager pointed out since it may be nil. Rather than holding on to a reference, I recommend editing the navigationBarHidden property always from within viewWillAppear as in this answer https://stackoverflow.com/a/27748007/2248638 . I have a BOOL navigationBarHidden property on my UIViewController base class so that I only need to set the property once for those view controllers which are hidden.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:self.navigationBarHidden animated:animated];
}
like image 26
skensell Avatar answered Dec 28 '22 07:12

skensell