【问题标题】:Hide statusBar when UITabBarController enters landscape modeUITabBarController 进入横向模式时隐藏状态栏
【发布时间】:2011-12-04 16:37:47
【问题描述】:

我创建了 UITabBarController 的子类,以便在横向模式下隐藏 tabBarstatusBar。我成功实现了隐藏/显示 tabBar 的代码,但 stausBar 让我发疯。我当前的实现 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 时,statusBar 是正确隐藏的,但框架不正确(顶部有 20px 的间隙)。如果我从这里返回纵向视图,布局将按预期恢复,如果我第二次以横向旋转,它将最终按预期工作(没有条,像素完美布局!)......从这一点开始,我可以将我的设备旋转 N 次,并且视图将始终以正确的方式呈现...... 那么,为什么我的代码第一次失败?!

您可能需要的额外信息:

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

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    我不敢相信,但解决方案真的很简单!我通过将 setStatusBarHidden:withAnimation: 从 didRotate... 移动到 willRotate... 来解决,实现如下:

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

    在我的情况下,不需要对新框架进行硬编码,因为我的视图使用了自动调整大小的掩码...视图将由 UIKit 自动正确呈现...真棒:)

    ... +1 to virushuo 引用 willRotateToInterfaceOrientation (我没有考虑到)

    【讨论】:

    • 如果您没有让“setStatusBarHidden”在 iOS7 上工作,请添加原始 plist 值“UIViewControllerBasedStatusBarAppearance”并将其设置为“NO”。这里有描述:stackoverflow.com/questions/18059703/…
    • 只有 didRotateFromInterfaceOrientation 在 iOS 8 上对我有帮助。
    • 最好的结果是使用willAnimateRotationToInterfaceOrientation。第一次旋转时不闪烁。
    【解决方案2】:

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

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

    • (void)setNavigationBarHidden:(BOOL)隐藏动画:(BOOL)动画 参数 隐 指定 YES 以隐藏导航栏或指定 NO 以显示它。 动画 如果要为可见性更改设置动画,请指定 YES,如果要立即显示导航栏,请指定 NO。 讨论 对于动画过渡,动画的持续时间由 UINavigationControllerHideShowBarDuration 常量中的值指定。

    可用性 适用于 iOS 2.0 及更高版本。

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

    【讨论】:

    • 我不想隐藏导航栏,而是隐藏状态栏!
    • 对不起。我认为问题是如何计算屏幕帧。尝试为 self.view 设置一个固定的全屏高度?
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多