我创建了一个 UITabBarController 的子类,以便在横向模式下隐藏一次 tabBar 和 statusBar。我成功地实现了隐藏/显示 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/