6

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?

ch_g
  • 63
  • 3

4 Answers4

4

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
}
JustSid
  • 24,711
  • 7
  • 72
  • 97
  • In fact, I encounter this problem when I use MPMoviePlayerController. I set moviePlayer fullscreen and want do something when MPCenteringNavigationBar showed – ch_g Oct 11 '12 at 04:43
  • "-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context" this function is called just twice. – ch_g Oct 11 '12 at 04:58
  • Is there a way to make this implementation work for [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide] ? – Shawn Feb 09 '16 at 19:32
  • This does not work. The UIApplication does not notify observers about changes to statusBarHidden. – Perrin Larson Dec 23 '16 at 21:29
1

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

lassej
  • 5,348
  • 4
  • 21
  • 31
0

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.

sumit
  • 13
  • 2
0

iOS 13

Since isStatusBarHidden and setStatusBarHidden are deprecated from iOS 13 you can check visibility of the status bar with UIStatusBarManager.isStatusBarHidden and Timer since it is not supported by KVO:

timer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true) { timer in
    if let statusBarManager = UIApplication.shared.delegate?.window??.windowScene?.statusBarManager {
        print(statusBarManager.isStatusBarHidden)
    }
}
iUrii
  • 4,212
  • 1
  • 12
  • 17