iOS 8 - Rotation makes statusBar disappear even in portrait mode after toggling controls

Phillip picture Phillip · Oct 3, 2014 · Viewed 13.6k times · Source

I'm having a lot of troubles with the new auto hiding of the status bar in iOS 8.

In my app, I've got a view in which when the user taps once, the navigation bar and the status bar disappear.

When in landscape, the status bar hides by itself and it's fine to me. I only need it in portrait mode.

But the problem is that when the device is landscape and the bar are shown, when the user taps twice to toggle the bar (so showing), and turns the device in portrait mode, the status bar is still hidden.

Basically I need to be able to hide the statusBar without interfere with its natural behavior on iOS 8, so I recap the scenario:

  • User enters said view with tabBar and NavigationBar and statusBar;
  • Taps once in the view and the bars disappear
  • User rotates the device, statusBar not appearing - OK, I want this
  • User taps again to show the bars - StatusBar still hidden, OK.
  • User rotates from landscape to portrait and..
  • statusBar still hidden - NOT OK.

MRW >
(source: mshcdn.com)

I've tried to adjust the statusBar on willRotate, but I made a mess in which the statusBar would be visible when it wasn't supposed to. Code I'm using:

- (BOOL)prefersStatusBarHidden
{
    return statusBarHidden;
}

-(void)toggleBars:(UITapGestureRecognizer *)gesture{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFromBottom;
    animation.subtype = kCATransitionFromTop;
    animation.duration = .2f;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
    
    
    BOOL toggleNavigationBar = self.navigationController.navigationBarHidden;
    [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
    [self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES];
    
    
    
    BOOL toggleTabHidden = self.tabBarController.tabBar.hidden;
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]isKindOfClass:[NSNull class]]){
        if([(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]boolValue])
        {
            [self.tabBarController.tabBar.layer addAnimation:animation forKey:nil];
            [self.tabBarController setHideTabBar:!toggleTabHidden animated:YES];
        }
    }
    
    statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
    [[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide];
    [self setNeedsStatusBarAppearanceUpdate];

    
    if (IS_IOS8){
        if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
            if (statusBarHidden){
                [[UIApplication sharedApplication] setStatusBarHidden:YES];
            }
        }
    }

}

I was thinking about setting a flag in which when the statusBar is hidden when in landscape and all the controls are there, on rotation it would trigger the statusBar. With no success, apparently..

Any help highly appreciated.

Answer

hybridcattt picture hybridcattt · Oct 6, 2014

Are you using UIViewController-based status bar appearance? If you implement prefersStatusBarHidden I assume you are.

Now,

[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation: UIStatusBarAnimationSlide];

is not supposed to work with UIViewController-based status bar appearance.

You need just to return different value from prefersStatusBarHidden method and call setNeedsStatusBarAppearanceUpdate to notify the app that returning value has changed.

So to change statusbar visibility you should just do

@property (nonatomic, assign) BOOL hideStatusBar;

- (BOOL)prefersStatusBarHidden 
{
    return self.hideStatusBar;
}

- (void)toggleBars:(UITapGestureRecognizer *)gesture 
{
    ... hide navbar and tabbar ...

    self.hideStatusBar = ! self.hideStatusBar;
    [self setNeedsStatusBarAppearanceUpdate];
}

And that's it!