Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

When the window becomes hidden, the status bar appears again.

Below, 2 screenshots of what I get on iOS9 with Xcode7 beta5.

Status bar while the custom window is hidden: Status bar while the custom window is hidden

Status bar while the custom window is visible: 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):

#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.

like image 547
Runo Sahara Avatar asked Sep 01 '15 04:09

Runo Sahara


2 Answers

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. (though it should return NO by default, normally)

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.

like image 96
Runo Sahara Avatar answered Nov 02 '22 05:11

Runo Sahara


The line _envWindow.windowLevel = UIWindowLevelStatusBar; puts your window at the same level (z-ordering) as the status bar. I think that what you want is UIWindowLevelNormal, so that the status bar appears over it.

like image 28
NRitH Avatar answered Nov 02 '22 06:11

NRitH