我试图制作一个类似于Photos应用程序的界面,其中状态栏和导航栏会淡入淡出,但我遇到了一个问题。如果我点击以隐藏界面,然后旋转设备,再次点击以将其带回,那么导航栏就会重新定位到状态栏下方(参见照片)。如果我随后旋转设备,导航栏会回到它的正确位置。我该如何解决这个问题?
- (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];
}
很遗憾,这有点烦人。当隐藏状态栏时,它基本上会打开那个空间供uiviewcontrollers使用。启用自动调整大小后,它将基本上移入该空间,因为它不知道您将要再次使用该空间。
最好的方法是手动调整视图大小,以便当状态栏再次出现时,您也将视图压缩,使其正确地定位在状态栏下方。
- (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];
}
我在隐藏导航栏时遇到了将alpha设置为0的问题。所以显然,在调用以下代码后:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
//hiding and showing to redraw navigationBar over the status bar
self.navigationController.navigationBarHidden = YES;
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.alpha = 0;
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];
}
通过以下方式解决了这个问题:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
self.navigationController.navigationBarHidden = YES;
self.navigationController.navigationBarHidden = NO;