Reputation: 37581
I have a problem which is caused by the status bar being hidden and then made to reappear - doing so changes the position of the navigation bar from being displayed below the status bar to being displayed behind it. i.e. the top part of the navigation bar can no longer be seen any more because the status bar is on top of it.
My program is constructed such that I have a root view controller that displays the status bar and the navigation bar, the view controller is contained within a navigation controller i.e.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.myTableViewController];
self.window.rootViewController = navController;
The view controller contains a table view and if the user selects a row then another controller is created and pushed to the navigation controller's stack.
This new view controller hides the status bar and also hides the navigation bar unless the user taps the screen, in which case the navigation bar appears (and its also translucent, which it is not in the parent view controller).
My problem is that when the user navigates from this view controller back to the table view controller then the navigation bar is now displayed behind the status bar.
I have the following code in the table view's viewWillAppear: which I thought ought to reset everything back to the way it was.
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden: NO withAnimation: UIStatusBarAnimationFade];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
Why does the navigation bar appear behind the status bar when the table view re-appears?
Thanks
Upvotes: 2
Views: 1990
Reputation: 343
I meet the same question, after try many goole on stack overflow and testing, found a way to solve the problem. I think the problem is cause by view's bounds changed (when you hide status bar for example, view's bounds will change)
(In my project not only hide the status bar but also hide navigation controller's navigation bar.)
below codes needed:
- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)test
{
// the point is UIApplicationWillEnterForegroundNotification notification process
// function should be called before all view controller delegate function being
// called, then we make all status bar and navigation bar show to restore the view's
// bounds, view contrller's view's bound should correct, then hide will not shift up
[self showStatusBarAndNaviBar];
[self performSelector:@selector(hideStatusBarAndNaviBar) withObject:self afterDelay:1.0];
}
//two custom function for use
- (void)hideStatusBarAndNaviBar
{
[UIView animateWithDuration:2.0f
delay:0.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^(void) {
self.navigationController.navigationBar.alpha = 0.0f;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
completion:^(BOOL finished){}];
}
- (void)showStatusBarAndNaviBar
{
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^(void) {
self.navigationController.navigationBar.alpha = 1.0f;
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
completion:^(BOOL finished){}];
}
Upvotes: 0
Reputation: 43330
I believe the status bar is about 20px high, so set the navigationBar's origin.y to be 20 and it should fix it. I know it's annoying, but that seems to be the only way to fix it in my experience.
Also check the position of the navigationBar when your app is coming out of a background state, I know my navigationBar reverted right back to being under the status bar when it came to the foreground.
Upvotes: 3