iOS 11 设备方向自转

iOS 11 Device Orientation Autorotation

对于 iOS 10 及以下,以下代码控制任何相应 UIViewController 的方向。我在我的部署信息中选择了 PortraitLandscape LeftLandscape Right,并且在我的 Info.plist 中有以下内容:

对于我的 VC 应该 旋转的我有以下代码,我说过,在 iOS11

- (BOOL)shouldAutorotate {
    [super shouldAutorotate];
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    return UIInterfaceOrientationMaskPortrait;
}

我已经在实际设备上测试过了,从 iOS 11 开始,它不起作用。

更奇怪的是,从 iOS 11 开始记录注册的设备方向告诉我设备 IS 纵向.. .当控制器以横向模式加载时...

代码:

// In viewDidLoad 
NSLog(@"orientation: %lu", [[UIDevice currentDevice] orientation]);

控制台输出:

2017-09-22 15:20:26.225196-0400 <APP_NAME>[2669:1628408] orientation: 1

这发生在构建之前向左或向右旋转设备和 运行 应用程序。



仅供参考:不,我的设备上没有旋转锁..

无论这是否被证明是 iOS 11 错误,我似乎偶然发现了这个问题的“解决方案”。无论出于何种原因,对于 iOS 11- (BOOL)shouldAutorotate return 更改为 YES 允许正确的方向...

对象

- (BOOL)shouldAutorotate {

    [super shouldAutorotate];
    
    if (@available(iOS 11, *)) return YES;
    return NO;

}

结合起来,我不得不手动检查屏幕尺寸以查看宽度是否大于或小于屏幕的假定高度。

width = self.view.frame.size.width, height = self.view.frame.size.height;
if (height < width) width = height, height = self.view.frame.size.width;

希望其他人找到这个“错误”的真正原因 Apple 更新他们的包以像所有以前的 iOS 版本一样处理旋转..

Swift 3.2+

override var shouldAutorotate: Bool {

    if #available(iOS 11.0, *) {
        // Anything else iOS 11 specific
        return true
    } 
    return false

}