Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status bar frame changes without any notification

I have registered to receive notifications about the status bar frame changes, but they are never received.

Here is how I register for the notification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appWillChangeStatusBarFrameNotification:)
                                             name:UIApplicationWillChangeStatusBarFrameNotification
                                           object:nil];

In some places in our app, we show/hide the status bar with:

[[UIApplication sharedApplication] setStatusBarHidden:maximize
                                        withAnimation:UIStatusBarAnimationSlide];

But it can also change size when personal hotspot is enabled or when in a phone call. Is there any way to get the actual status bar frame when it changes?

This question implies that notifications don't work due to an SDK bug, at least for orientation changes. Is that the reason? Is there any workaround?

like image 880
progrmr Avatar asked Oct 07 '12 21:10

progrmr


1 Answers

I know this question was posted a while back, but this problem is an annoying one! The UIApplicationWillChangeStatusBarFrameNotification and UIApplicationDidChangeStatusBarFrameNotification notifications only fire with orientation change and in-call status bar height changes.

I solved this by writing my own setStatusBarHidden category function that I use instead of the normal UIApplication function. Unfortunately (as @progrmr pointed out), because the status bar height could be 20 pixels or 40 pixels (and we have no idea of knowing what a hidden status bar's frame will become until after it is unhidden), we can only reliably fire off a single notification with the correct userInfo (UIApplicationDidChangeStatusBarFrameNotification). Here's what I did:

@implementation UIApplication (statusBar)

- (void)setStatusBarHiddenWithNotification:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation
{
    if (self.statusBarHidden == hidden) return;

    [self setStatusBarHidden:hidden withAnimation:animation];
    [NSNotificationCenter.defaultCenter postNotificationName:UIApplicationDidChangeStatusBarFrameNotification
                                                      object:nil
                                                    userInfo:@{UIApplicationStatusBarFrameUserInfoKey: [NSValue valueWithCGRect:self.statusBarFrame]}];
}

@end

It's slightly hacky, but I like it because I can use the same notification observer to listen for both in-call status bar frame changes and my manual setStatusBarHidden frame changes.

Hope this helps someone!

like image 81
Mr. T Avatar answered Nov 05 '22 23:11

Mr. T