How can I detect UIStatusBar hide and show?

六眼飞鱼酱① 提交于 2019-12-23 11:50:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!