【问题标题】:StatusBar orientation wrong when receiving orientation changed notification收到方向更改通知时状态栏方向错误
【发布时间】:2013-12-13 08:34:14
【问题描述】:

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

[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(重新定位视图) 名称:UIDeviceOrientationDidChangeNotification 对象: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

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

【问题讨论】:

    标签: ios objective-c uideviceorientation


    【解决方案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 
        }
    
    }
    

    【讨论】:

    • 似乎有效,但现在我对该方法进行了大约 6 次调用。 3/ 视图控制器。为什么?
    • repositionView 方法会在应用的用户界面方向改变时被调用。
    • 不,一次轮换将向所有 viewCONtrollers 发送 3 个通知
    • 如果您在每个视图控制器中添加观察者。它将被调用 3 次意味着一次在所有视图控制器中。每当应用程序中的方向发生变化时,都会调用此方法。
    • 不,在日志中它显示了 2 个视图控制器的 6 个通知 ... 3/ 控制器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多