Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect UIStatusBar hide and show?

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?

like image 794
ch_g Avatar asked Oct 11 '12 04:10

ch_g


2 Answers

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
}
like image 188
JustSid Avatar answered Oct 19 '22 21:10

JustSid


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).

like image 35
lassej Avatar answered Oct 19 '22 22:10

lassej