iphone - 当 UITabBarController 进入横向模式时隐藏 statusBar

标签 iphone objective-c ios

我创建了一个 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 的间隙)。如果我从这里返回纵向 View ,布局将按预期恢复,如果我第二次横向旋转,它最终将按预期工作(没有条形图,像素完美布局!)...从这一点开始我可以旋转我的设备 N 次, View 将始终以正确的方式呈现...... 那么,为什么我的代码第一次失败?!

您可能需要的额外信息:

  • 根选项卡 Controller 是 UINavigationControllers
  • 我的所有嵌套 View Controller 都已正确配置以支持方向更改
  • 我正在使用 iOS 5 进行测试

最佳答案

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

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

在我的例子中,不需要对新框架进行硬编码,因为我的 View 使用了自动调整大小的 mask ... View 将由 UIKit 自动正确渲染...太棒了 :)

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

关于iphone - 当 UITabBarController 进入横向模式时隐藏 statusBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8376791/

相关文章:

iphone - 如何使用 HTTPHeaderField 创建 NSURLRequest

iphone:从数组中添加对象在重新初始化 nsmutablearray 后不添加

ios - 如何使用 Parse 保存数组?

iphone - 为什么不能重用任何UITableView单元格?

ios - 当通过代码在 iOS 上显式加载嵌入式框架时,alloc 和 class 返回 nil 对象

IOS - Swift - 向 BarButtonItem 的自定义 View 添加目标和操作

iphone - 如何在 iPhone 或 iPad 中获取进程 ID?

ios - 有没有办法让 TextField 输入像 ios 中的谷歌钱包输入一样?请参阅所附照片以了解我的需要

objective-c - 比较 isEqual 中的 nil 属性

ios - 静态 TableViewCell 中的 UICollectionView