接收方向更改通知时StatusBar方向错误

时间:2013-11-27 15:48:05

标签: ios objective-c uideviceorientation

我注册了2个视图控制器以接收设备方向更改通知。 `

  

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

`

在重新定位视图方法中,我有:

   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 
    }

当方向改变时,它们都在内存中

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

只有一个接收到正确的方向。 为什么是这样 ?我该如何解决?

1 个答案:

答案 0 :(得分:5)

使用 UIApplicationDidChangeStatusBarOrientationNotification 代替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 
    }

}