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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…