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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With