26

Say a user is in a View Controller and wants to enter a "full screen" type mode where the status bar is hidden, under iOS 6 it was a simple method call to hide/show the status bar, but no matter what it seems to persist under iOS 7.

I've seen solutions like this:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

But that doesn't allow it to be toggled at runtime. (It doesn't accept any arguments.)

In my info.plist I have View controller-based status bar appearance set to NO.

I'm at wits end. How do I hide it?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

5 Answers5

74

Swift 4

show:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false

hide:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = true



Objective-c

Well here's one way of doing this:

in myViewController.h

@interface myViewController : UIViewController {
    BOOL shouldHideStatusBar;
}

Then in myViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    shouldHideStatusBar = YES;
}

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar;
}

and let's say when I touch the screen it should show the status bar now. You'll need to call: setNeedsStatusBarAppearanceUpdate specifically to get this working and then a switch (bool in this case) to show/hide.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    shouldHideStatusBar = (shouldHideStatusBar)? NO: YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

setNeedsStatusBarAppearanceUpdate

This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.

prefersStatusBarHidden:

Return Value A Boolean value of YES specifies the status bar should be hidden. Default value is NO.

Discussion If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate method.

To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.


If you plan on your app working with iOS 6 as well might want to look at this post

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • 1
    Do I need to change the view controller based status bar line in my PLIST? – Doug Smith Nov 08 '13 at 21:27
  • 2
    Why wrap the call to `setNeedsStatusBarAppearanceUpdate` in a `performSelector` call, and why call `prefersStatusBarHidden`? – Tommy Nov 08 '13 at 21:35
  • 1
    If I add the `preferStatusBarHidden` method and return YES it has no effect currently. – Doug Smith Nov 08 '13 at 21:40
  • You do not have to do anything to your PLIST. I'm unsure why you're having any issues, you if you exactly what I've done here it works 100%. The reason behind the `performSelector:` is because it allows you to send message that aren't determined until runtime. Which from my understanding, perfersStatusBarHidden return value is a runtime function. – John Riselvato Nov 09 '13 at 19:46
  • 1
    You don't need to call `-prefersStatusBarHidden` yourself, the system will call that to figure out whether it needs to display it. Also you shouldn't need to wrap anything in `performSelector` it should work fine without. – Evan Nov 13 '13 at 03:46
  • 1
    For a correct switching on/off of status bar in -(void)touchesBegan: withEvent: use shouldHideStatusBar = (shouldHideStatusBar)? NO: YES; – StanislavK Nov 17 '13 at 11:47
  • This works great! I was having issues just using the `preferrersStatusBarHidden` flags and it wasn't updating properly. I switched to using flags as well as `setNeedsStatusBarAppearanceUpdate` and this fixed my update issue. – Bot Mar 25 '14 at 16:39
3

I was having trouble with some of the other answers in iOS 8 so I did a little more research and found: [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];. You can then enable/disable the animation.

Johann Burgess
  • 580
  • 6
  • 17
2

Recommendation

For iOS7 support, I'd suggest you turn View controller-based status bar appearance back to YES. This will allow you to control the status bars in code. The iOS transition guide (link) provides other options for managing the status bar appearance (there is not just a single fix here but a number of settings that will ensure you get what you want). Also be aware that even if you set the appearance in code you will want to update your storyboards or nib files to match your default appearance (otherwise you may see the status bar flash temporarily depending on where you set the code to update the status bar). I would recommend that you set the code before the view appears.

After setting your plist property to YES: Be sure this method exists in UIViewController where you want to status bar to disappear:

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

Showing the Status Bar

Return No if you'd like the status bar to appear in each of your view controllers.

There can be a number of other status bar related issues:

Alternative Approach

According to Apple's Documentation you can use another method of managing the status bar by leveraging the UIApplication method noted in the reference below (link). setStatusBarHidden is still viable when using this approach.

Option #2

This option will allow you to continue to use the UIApplication class properties if you follow the plist setting above.

UIApplication Status

Samples

You can find code samples of the status bar alternative option mentioned above in:

Tommie C.
  • 12,895
  • 5
  • 82
  • 100
0

If the view controller that is on screen is the root view controller then you should just be able to implement the function

- (BOOL)prefersStatusBarHidden {
    return _showStatusBar;
}

with _showStatusBar being a BOOL, then whenever you change that property call [self setNeedsStatusBarAppearanceUpdate]

If the view controller is being held inside of something else i.e. a UINavigationController then you need to implement the method - (UIViewController *)childViewControllerForStatusBarHidden on the parent controller first and return the currently presented view controller instance.

This is all with View controller-based status bar appearance set to YES

Evan
  • 750
  • 7
  • 13
-1

i think this will work under summary page of your app or else reply me enter image description here

visibility ---check that box

Muthu Ram
  • 993
  • 6
  • 15
  • That doesn't allow me to change it on the fly, as stated in the original post. – Doug Smith Nov 09 '13 at 18:32
  • 1
    @Doug Smith **can you add this code in your appdelegate** `self.window.frame =  CGRectMake(0,0,self.window.frame.size.width,self.window.frame.size.height);` – Muthu Ram Nov 10 '13 at 00:32