Search code examples
iosiphonexcode6ios9statusbar

Runtime Hide / Show Status Bar iOS 9+


I have an application where it is important to hide / show the status bar and switch its style on the fly. Previously, it was very easy with the following calls:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

But they have been deprecated, and I don't quite understand how the new methods work. I was able to set the style and initial visibility by adding the following line to the plist:

 View controller-based status bar appearance = YES

And then adding the following methods to my view controller:

 - (UIStatusBarStyle)preferredStatusBarStyle
 {
     return UIStatusBarStyleDefault;
 }

 - (BOOL)prefersStatusBarHidden
 {
     return NO;
 }

This works fine on the view controllers as a whole (as a static setting that gets called when the view is initialized), but I am unable to change them on the fly, which is what I need.

How could I achieve this?


Solution

  • I hate to answer my own question, but after doing some digging, I found how to call the method manually. First, I created a BOOL variable that can be switched on the fly and then returned in the prefersStatusBarHidden method.

     - (BOOL)prefersStatusBarHidden
     {
         return isStatusBarHidden;
     }
    

    Then, whenever I wanted to hide/show the status bar, I changed the value of isStatusBarHidden and forced the view to check if its staus bar needs to be updated like so:

     isStatusBarHidden = NO;
     [self setNeedsStatusBarAppearanceUpdate];
    

    Works perfectly for me on devices running iOS9 and above.