Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under iOS 7, how do I hide and show status bar on the fly (whenever I want to)

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?

like image 266
Doug Smith Avatar asked Nov 04 '13 18:11

Doug Smith


1 Answers

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

like image 135
John Riselvato Avatar answered Sep 20 '22 19:09

John Riselvato