Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 312
Phillip Avatar asked Oct 03 '14 09:10

Phillip


People also ask

How do I get my iPhone screen to rotate again?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.

How do I force an app to landscape in IOS?

On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.

How do I get my iPhone screen to rotate text messages?

Swipe up from the bottom edge of your screen to open Contol Center. Tap the Portrait Orientation Lock button to make sure that it's off.

How do I rotate my iPhone screen 180 degrees?

Swipe up from the bottom of your screen. This will open the Control Center, from which you can enable or disable rotation lock.


1 Answers

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!

like image 184
hybridcattt Avatar answered Sep 23 '22 19:09

hybridcattt