Search code examples
iosipadios8statusbarappearance

prefersStatusBarHidden and Black bar in window after backgrounding an iPhone only app on iPad iOS 8


When running my iPhone only app on an iPad with iOS 8 I've had a black bar appearing in my application's window. I can reproduce it reliably by backgrounding the app in landscape, then re-opening in portrait. Everything is fine on an iPhone.

To try and eliminate some extra factors I've replaced the initial viewcontroller in my storyboard with an xcode default new viewcontroller file. The black bar persists on my otherwise blank white window.

When the app is running "normally", the window looks to be padded with a grey area at the top. The black bar appears to "fall out" of this grey area and end up in the middle of my window.

I've tried setting "View controller-based status bar appearance" to NO. Then, adding the following to prefersStatusBarHidden gets rid of the annoying bar, but then my navbar gets cropped from the top!

- (BOOL)prefersStatusBarHidden
{
 UIDeviceOrientation orientation = [[UIApplication sharedApplication]    statusBarOrientation];
  switch(orientation) {
    case UIDeviceOrientationLandscapeLeft:
    case UIDeviceOrientationLandscapeRight:
      return NO;

    default:
      return YES;
  }
}

I'm wondering if any of you know the cause/ cure for this?

See below for screen shots...

The Black bar!

Black bar

App running normally, note padded grey area at the top of the screen.

App running "normally"


Solution

  • I fixed my issues by...

    1. Setting "View controller-based status bar appearance" to NO.

    2. Testing for iOS version and setting my NavBar background image accordingly...

      NSString *version = [[UIDevice currentDevice] systemVersion];
      bool isAtLeastiOS_8 = [version floatValue] >= 8.0;
      
      if(isAtLeastiOS_8) {
        UIImage *bgImagePortrait = [UIImage imageNamed:@"NavBarPortrait.png"];    
        [self.navigationBar setBackgroundImage:bgImagePortrait forBarMetrics:UIBarMetricsDefault];
      } else {
        UIImage *bgImagePortrait = [UIImage imageNamed:@"NavBarPortrait.png"];
        UIImage *bgImageLandscape = [UIImage imageNamed:@"NavBarLandscape.png"];
      
        [[UINavigationBar appearance] setBackgroundImage:bgImagePortrait
                                     forBarMetrics:UIBarMetricsDefault];
      
        [[UINavigationBar appearance] setBackgroundImage:bgImageLandscape
                                     forBarMetrics:UIBarMetricsLandscapePhone];
      }
      
    3. Accepting the loss of the status bar in landscape mode on iOS 8