简体   繁体   English

iOS9:自定义UIWindow使状态栏消失

[英]iOS9: Custom UIWindow makes status bar disappear

When I create a custom UIWindow in iOS9, the window becomes visible on the screen, but the status bar suddenly disappears. 当我在iOS9中创建自定义UIWindow时,窗口会在屏幕上显示,但状态栏突然消失。

When the window becomes hidden, the status bar appears again. 当窗口隐藏时,状态栏会再次出现。

Below, 2 screenshots of what I get on iOS9 with Xcode7 beta5. 下面是我在iOS9上使用Xcode7 beta5获得的2个截图。

Status bar while the custom window is hidden: 隐藏自定义窗口时的状态栏: 隐藏自定义窗口时的状态栏

Status bar while the custom window is visible: 自定义窗口可见时的状态栏: 自定义窗口可见时的状态栏 (The whole screen moves to the top.) (整个屏幕移到顶部。)

This is the code I'm using (which worked well on iOS8): 这是我正在使用的代码(在iOS8上运行良好):

#define DEBUG_SHOWENV_HEIGHT 20.0f

@interface AppDelegate ()
@property (nonatomic) UIWindow*     envWindow;
@end

-(UIWindow*)envWindow
{
    if (_envWindow == nil)
    {
        // Create the window
        _envWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT)];
        _envWindow.rootViewController = [[UIViewController alloc] init]; // added since iOS9 to avoid the assertion
        _envWindow.userInteractionEnabled = NO;
        _envWindow.windowLevel = UIWindowLevelStatusBar;
        _envWindow.backgroundColor = [UIColor colorWithRed:0.243 green:0.471 blue:0.992 alpha:0.8];

        // Make a label
        UILabel* labelEnv = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 0.0f, _envWindow.bounds.size.width - 16.0f, DEBUG_SHOWENV_HEIGHT)];
        labelEnv.font = [UIFont boldSystemFontOfSize:12.0f];
        labelEnv.textColor = [UIColor whiteColor];
        labelEnv.text = @"DEVELOP ENVIRONMENT";
        [_envWindow addSubview:labelEnv];
    }
    return _envWindow;
}

// ==== in applicationDidBecomeActive

// Show the window 2 seconds then hide it.
[self.envWindow.layer removeAllAnimations];
self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
self.envWindow.hidden = NO;
[UIView animateWithDuration:0.25f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height - DEBUG_SHOWENV_HEIGHT, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
                 }
                 completion:^(BOOL finished) {

                     if (finished)
                     {
                         [UIView animateWithDuration:0.25f
                                               delay:2.0f
                                             options:UIViewAnimationOptionCurveEaseOut
                                          animations:^{
                                              self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
                                          }
                                          completion:^(BOOL finished) {

                                              if (finished)
                                              {
                                                  self.envWindow.hidden = YES;
                                              }
                                          }];
                     }
                 }];

I'd appreciate any help. 我很感激任何帮助。

Solved. 解决了。 I needed to implement this method in the root view controller: 我需要在根视图控制器中实现此方法:

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

For some reasons, the root view controller in this UIWindow hid the status bar. 出于某些原因,此UIWindow中的根视图控制器隐藏了状态栏。 (though it should return NO by default, normally) (虽然默认情况下它应该返回NO,通常)

So instead of doing: 所以不要这样做:

_envWindow.rootViewController = [[UIViewController alloc] init]; // added since iOS9 to avoid the assertion

I created my own view controller with prefersStatusBarHidden overridden. 我创建了自己的视图控制器,并覆盖了prefersStatusBarHidden。

The line _envWindow.windowLevel = UIWindowLevelStatusBar; _envWindow.windowLevel = UIWindowLevelStatusBar; puts your window at the same level (z-ordering) as the status bar. 将窗口置于与状态栏相同的级别(z排序)。 I think that what you want is UIWindowLevelNormal , so that the status bar appears over it. 我认为你想要的是UIWindowLevelNormal ,所以状态栏出现在它上面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM