当UITabBarController进入横向模式时隐藏状态栏



我创建了UITabBarController的子类,以便在横向模式下隐藏tabBarstatusBar。我成功地实现了隐藏/显示标签栏的代码,但是状态栏让我抓狂。我目前的实施工作100%,但不是第一次轮换,我无法找出原因。代码如下:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
  BOOL hide = (fromInterfaceOrientation == UIInterfaceOrientationPortrait || 
                         fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
  [[UIApplication sharedApplication] setStatusBarHidden:hide withAnimation:UIStatusBarAnimationNone];
  CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
  [self.view setFrame:mainFrame];
}

在实践中,我第一次旋转我的iPhone,状态栏是正确隐藏的,但框架是不正确的(它有20px的间隙在顶部)。如果我从这里返回到纵向视图,布局将恢复如预期,如果然后我在横向旋转第二次,它将最终按预期工作(没有条,像素完美的布局!)…从这个点开始,我可以旋转我的设备N次,视图将始终以正确的方式呈现……那么,为什么我的代码第一次失败?

你可能需要的额外信息:

  • 根选项卡控制器是UINavigationControllers
  • 我的所有嵌套视图控制器都正确配置为支持方向更改
  • 我正在测试使用iOS 5

我简直不敢相信,但解决方案真的很简单!我通过移动setStatusBarHidden:withAnimation:从didroate解决…willRotate……,实现如下:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  BOOL show = (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
                 toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
  [[UIApplication sharedApplication] setStatusBarHidden:!show withAnimation:UIStatusBarAnimationNone];
}

在我的例子中,没有必要硬编码新框架,因为我的视图使用了自动调整大小的蒙版…视图将被UIKit自动渲染…棒:)

…+1对virushuo引用willRotateToInterfaceOrientation(我没有考虑到)

尝试UINavigationController类方法setNavigationBarHidden:animated: in willRotateToInterfaceOrientation.

setNavigationBarHidden:动画:设置导航栏是否隐藏。

  • (void) setNavigationBarHidden: (BOOL)隐藏的动画:(BOOL)动画参数隐藏的指定YES隐藏导航栏,或指定NO显示导航栏。动画如果您想让可见性变化产生动画效果,请指定YES;如果您想让导航栏立即显示,请指定NO。讨论对于动画转场,动画的持续时间是由UINavigationControllerHideShowBarDuration常量中的值指定的。

可用性可在iOS 2.0及更高版本中使用。

http://developer.apple.com/library/ios/文档/uikit/引用/UINavigationController_Class/引用/Reference.html

最新更新