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

13,755

Solution 1

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!

Solution 2

This worked for me:

- (void)viewWillLayoutSubviews {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

Solution 3

This is the swift (3.0) version of that prefersStatusBarHidden

override var prefersStatusBarHidden: Bool{
    return false
}

You just need to add it to your ViewController

Share:
13,755

Related videos on Youtube

Phillip
Author by

Phillip

Updated on September 15, 2022

Comments

  • Phillip
    Phillip almost 2 years

    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.

  • Phillip
    Phillip over 9 years
    Thanks for it! Pointed me to the right path! The only thing I had to do was adding another bool to check whether the orientation should show the statusBar. It worked like a charm!
  • hybridcattt
    hybridcattt over 9 years
    Just remember this only works from iOS7, for older versions [[UIApplication sharedApplication] setStatusBarHidden...] must be used. Glad to help!
  • Phillip
    Phillip over 9 years
    Fortunately I only need to support iOS 7 and higher, so it's fine! Thanks again.
  • Dmitry
    Dmitry over 9 years
    How can I do the same for UITabBarController?
  • Peter Kreinz
    Peter Kreinz over 4 years
    "didRotateFromInterfaceOrientation" is deprecated since iOS 8.0