[objective-c]显示状态栏方向错误时接收方向更改通知

标签: ios Objective-C
发布时间: 2016/10/2 15:06:36
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 2 视图控制器已注册接收设备方向变化通知。 `

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repositionView) 名称︰ UIDeviceOrientationDidChangeNotification 对象︰ 零];

`

我有复位视图方法︰

   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:

使用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 
    }

}
官方微信
官方QQ群
31647020