iosobjective-cuideviceorientation

StatusBar orientation wrong when receiving orientation changed notification


I have 2 view controller registered to receive a Device orientation changed notification. `

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repositionView) name:UIDeviceOrientationDidChangeNotification object:nil];

`

In the reposition view method I have :

   UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIInterfaceOrientationIsLandscape(orientation)) {
        NSLog(@"view (%@) will set orientation landscape",self);
// do smt
    } else {
          NSLog(@"view (%@) will set orientation portrait",self);
//do smt else 
    }

They are both in memory when the orientation changes

2013-11-27 17:37:54.277 App[13782:70b] view (<ViewController: 0xbf09950>) will set orientation landscape
2013-11-27 17:37:54.286 App[13782:70b] view (<ViewController: 0xc095e40>) will set orientation portrait
2013-11-27 17:38:17.318 App[13782:70b] view (<ViewController: 0xbf09950>) will set orientation portrait
2013-11-27 17:38:20.123 App[13782:70b] view (<ViewController: 0xc095e40>) will set orientation landscape

Only one receives the correct orientation. Why is this ? And how do I fix it?


Solution

  • Use UIApplicationDidChangeStatusBarOrientationNotification instead of UIDeviceOrientationDidChangeNotification.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repositionView:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    
    -(void)repositionView:(NSNotification *)notification
    {
    UIInterfaceOrientation orientation = [[[notification userInfo] objectForKey: UIApplicationStatusBarOrientationUserInfoKey] integerValue];
    
    if (UIInterfaceOrientationIsLandscape(orientation)) {
            NSLog(@"view (%@) will set orientation landscape",self);
    // do smt
        } else {
              NSLog(@"view (%@) will set orientation portrait",self);
    //do smt else 
        }
    
    }