问题
I'm trying to detect hidden and show of iPhone's UIStatusBar but failed. Are there any solution can help me, like KVO or something else?
回答1:
You can observe the statusBarHidden
property of the shared UIApplication
instance.
Simple example:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// Do something here...
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL];
[[UIApplication sharedApplication] setStatusBarHidden:YES]; // Will notify the observer about the change
}
回答2:
From iOS 11 and up you can subclass the UIView of the view controller and override safeAreaInsetsDidChange
:
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
// adapt your view
}
Your view must share the top rect with the status bar for this to work. (But if it doesn't, you probably wouldn't need to detect changes anyway).
回答3:
In UIApplication class there is a property statusBarHidden...this tell status bar hidden or not...if it return YES mean status bar is hidden...try this.
来源:https://stackoverflow.com/questions/12832126/how-can-i-detect-uistatusbar-hide-and-show