我有一个基于 UITabBarController 的应用程序,它的部署目标是 iOS5.1(所以,我没有使用 iOS7 的基于视图控制器的状态栏外观功能)。我想隐藏第一个选项卡上的状态栏。它在第一次启动时工作正常(视图被拉伸到 431 点 = 480 - 49 点的 tabBar),但在更改选项卡并返回第一个屏幕后,状态栏被隐藏,但有一个黑色矩形最佳。
我的代码(AppDelegate + 第一个选项卡 ViewController):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
application.statusBarHidden = YES;
return YES;
}
- (void) loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSLog(@"load View %@", NSStringFromCGRect(self.view.frame));
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"view did load %@", NSStringFromCGRect(self.view.frame));
...
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"view willAppear %@", NSStringFromCGRect(self.view.frame));
....
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"view didAppear %@", NSStringFromCGRect(self.view.frame));
....
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
....
self.view = nil;
}
这是跟踪输出:
加载视图 {{0, 0}, {320, 480}}
视图确实加载了 {{0, 0}, {320, 480}}
视图将出现 {{0, 0}, {320, 431}}
查看 didAppear {{0, 0}, {320, 431}}
加载视图 {{0, 0}, {320, 480}}
视图确实加载了 {{0, 0}, {320, 480}}
视图将出现 {{0, 0}, {320, 411}}
查看 didAppear {{0, 0}, {320, 411}}
正如在日志中看到的,状态栏高度从viewWillAppear:
方法中的视图高度中减去,当它第二次被触发返回到第一个选项卡屏幕时。为什么会这样?任何关于如何修复/绕过的建议将不胜感激!
PS 我已经尝试插入[[UIApplication sharedApplication] setStatusBarHidden:YES];
所有视图回调(viewDidLoad、viewWillAppear 等),但它没有帮助!我也尝试了以下技巧但没有运气:1)隐藏状态栏后显示/关闭虚拟模式视图控制器;2) [self.view sizeToFit];
+[self.view setNeedsLayout];