iOS8 + UIDeviceOrientationDidChangeNotification 没有调用

7

在我的工作应用程序 (iOS 7.0.1) 中,会调用 UIDeviceOrientationDidChangeNotification

现在在 iOS 8.0 中运行此应用程序时,UIDeviceOrientationDidChangeNotification 不会被调用。

已经尝试了很多次,但没有成功。

编辑-1:

我使用下面的代码添加了方向更改的观察器。在 iOS 7 中完美运行,但在 iOS 8 上无效。

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

在不更改以前的代码的情况下,它在iOS 8.0上开始工作了。添加观察者并很快就好了。 - iGW
@iGW 我也是这么做的。 - Kampai
@iGW 我明白了,这是我使用的第三方库出现了问题。谢谢您的帮助。 - Kampai
@iGW 我仍然遇到一个问题,已经编辑了我的问题。 - Kampai
我在我的任何项目中都没有使用过这个库。会去检查一下。 - iGW
显示剩余3条评论
3个回答

15

在我的情况下,经过多次搜索后,我发现我犯了一个可笑的错误,即我的设备竖屏方向锁定已打开。

请确认设置中的设备方向锁定关闭。

这可能会帮助到其他人。


3
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

>

-(void)didRotateDeviceChangeNotification:(NSNotification *)notification
{
    UIInterfaceOrientation newOrientation =  [UIApplication sharedApplication].statusBarOrientation;
    if ((newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight))
    {

    }
}

我找到了答案,这些旋转方法在iOS 8.0中已经被弃用。请查看UIViewController.h类。 - Kampai
@Kampai,我认为设备方向的通知尚未被弃用。 - Yuchen

2

Swift 3:

在viewDidLoad中添加对UIDeviceOrientationDidChange的观察者

        NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

选择器方法是这样的:

    func orientationChanged(notification: Notification) {

    }

不要忘记在deinit方法中添加Remove observer

NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接