开发者

UINavigationBar Appears Under StatusBar

I'm attempting to make an interface similar to the Photos app where the status bar & navigation bar fade in/out but I'm running into a problem. If I tap to hide the inte开发者_StackOverflowrface then rotate the device, then tap to bring it back up, then the navigation bar is repositioned underneath the status bar (see photo). If I then rotate the device, the navigation bar goes back into it's proper place. How can I fix this?

UINavigationBar Appears Under StatusBar


This may come a little late but I have just had the EXACT same problem. I was desiging a reader view in full-screen with transparent status bar, navigation bar and toolbar that you could fade in and out by tapping in the center of the screen.

The way I have managed to fix it is really simple, basically the core of the problem if this : When you rotate the view and the NavigationController recalculates its new position, it thinks it should be at the top of the window because the status bar is hidden. When you show both the status bar and the navigation bar after that, they are overlapping.

The way to fix this is really easy, simply keep a BOOL to remember if your overlay is shown or hidden, and implement both willRotateToInterfaceOrientation and willAnimateRotationToInterfaceOrientation in your ViewController.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (!isOverlayShowing)
    {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    }
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (!isOverlayShowing)
    {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

By quickly showing and hiding the StatusBar in these two methods, the StatusBar is shown at the exact moment the NavigationBar recalculates its position. I don't know if this is the best implementaiton to fix this problem, but so far this method works and does not create any flickering on screen and is very smooth.

I hope someone else with my problem may stumble upon this post and find this easy solution to this problem.


Unfortunately, it's kinda annoying. When hiding the statusbar, it basically opens up that space for uiviewcontrollers to use. With autoresizing on, it'll basically move into that space because it has no idea that you're going to want to use that space again.

Your best bet is to manually resize the view so that when the status bar appears again, you also scrunch the view down so that it is correctly positioned below the status bar.


Another (simpler) solution is to reset the top-level view's frame when making the status bar visible again; e.g.

- (void)exitFullScreenMode
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    // work-around for navigation bar appearing under status bar - must be called before -setNavigationBarHidden:
    self.view.window.rootViewController.view.frame = [UIScreen mainScreen].applicationFrame;

    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


I had problems setting the alpha = 0 for the navigationBar to hide it. So apparently, after calling:

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

You can remove the navigationBar by calling hidden and the re-inserting it into the view and then setting the alpha = 0:

//hiding and showing to redraw navigationBar over the status bar
self.navigationController.navigationBarHidden = YES;
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.alpha = 0;

This will solve the extra space that the status bar used to occupy.


if ( self.navigationController.navigationBarHidden )
{
    /******* Cancel Fullscreen ****/
    if ( UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad )
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController setToolbarHidden:NO animated:YES];
}
else {
    /******* Enable Fullscreen ****/
    if ( UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad )
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];
}


Fixed this issue by:

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

    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBarHidden = NO;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜