6

I want to find out when the status bar rotates. Receiving a screen rotation notification can confuse matters with orientations such as 'face up' and 'face down'. Managing rotation based on the orientation of the status bar is therefore the simplest and cleanest way of doing it. How do i get a notification when the orientation changes?

Andrew
  • 15,935
  • 28
  • 121
  • 203

2 Answers2

12

You need to register for the UIApplicationDidChangeStatusBarOrientationNotification notification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIInterfaceOrientation orient = [notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];

    // handle the interface orientation as needed
}

Note that this approach never results in the "face up" or "face down" device orientations since this only deals with interface orientations.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    This actually gave me the previous orientation instead of the current new one. Switching to `[[UIApplication sharedApplication] statusBarOrientation]` gave me the new status bar orientation – Gordon Tucker Apr 11 '14 at 20:36
-1
//register to receive orientation notifications somewhere:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];


    -(void)detectOrientation{

        switch ([[UIDevice currentDevice] orientation]) {

            case UIDeviceOrientationFaceDown:{
            }
            break;

            default:{
            }
            break;

        }
    }

If it's not working, check orientation lock! Wasted hours on that.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148