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

Posted

技术标签:

【中文标题】iOS9:自定义 UIWindow 使状态栏消失【英文标题】:iOS9: Custom UIWindow makes status bar disappear 【发布时间】:2015-11-26 05:04:48 【问题描述】:

当我在 ios9 中创建自定义 UIWindow 时,窗口在屏幕上变得可见,但状态栏突然消失了。

当窗口被隐藏时,状态栏再次出现。

下面是我使用 Xcode7 beta5 在 iOS9 上获得的 2 个屏幕截图。

自定义窗口隐藏时的状态栏:

自定义窗口可见时的状态栏: (整个屏幕移动到顶部。)

这是我正在使用的代码(在 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;
                                              
                                          ];
                     
                 ];

我将不胜感激。

【问题讨论】:

为什么你有一个自定义窗口,而不仅仅是一个UIViewController 因为我希望它在应用程序激活时显示(即使在转换等期间) 【参考方案1】:

解决了。 我需要在根视图控制器中实现这个方法:

- (BOOL)prefersStatusBarHidden

    return NO;

由于某些原因,此 UIWindow 中的根视图控制器隐藏了状态栏。 (虽然它应该默认返回 NO,通常)

所以不要这样做:

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

我创建了自己的视图控制器,并重写了 prefersStatusBarHidden。

【讨论】:

【参考方案2】:

_envWindow.windowLevel = UIWindowLevelStatusBar; 行将您的窗口置于与状态栏相同的级别(z 顺序)。我想你想要的是UIWindowLevelNormal,这样状态栏就会出现在它上面。

【讨论】:

没有解决我的问题。当状态栏不存在时,视图内容会转移到屏幕顶部,这似乎不仅仅是一个绘制顺序问题。

以上是关于iOS9:自定义 UIWindow 使状态栏消失的主要内容,如果未能解决你的问题,请参考以下文章

添加新 UIWindow 时以编程方式隐藏状态栏?

ios9 键盘使uiwindow上移

将 UIView 或 UIWindow 放在状态栏上方

自定义监听状态栏点击

如何使定位服务图标从状态栏中消失?

iOS 8 - 切换控件后,即使在纵向模式下,旋转也会使状态栏消失