iOS 11设备方向自动旋转

Wil*_*ich 8 objective-c orientation ios swift ios11

对于iOS 10及更低版本,以下代码控制任何相应的方向UIViewController.我选择了Portrait,Landscape LeftLandscape Right我的部署信息,并在我下面的Info.plist:

在此输入图像描述

对于那些根本应该旋转的VC,我有以下代码,我说过,它在iOS 11之前工作

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

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

我已经在实际设备上测试了这个,而且从iOS 11开始它不起作用.

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

码:

// In viewDidLoad 
NSLog(@"orientation: %lu", [[UIDevice currentDevice] orientation]);
Run Code Online (Sandbox Code Playgroud)

控制台输出:

2017-09-22 15:20:26.225196-0400 <APP_NAME>[2669:1628408] orientation: 1
Run Code Online (Sandbox Code Playgroud)

这可能是在构建和运行应用程序之前向左或向右旋转设备.


  • 这个错误的原因是什么?如果有人知道请帮助..

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

Wil*_*ich 6

无论这是否是一个iOS 11的错误,我似乎偶然发现了这个问题的"解决方案".无论出于何种原因,对于iOS 11,更改- (BOOL)shouldAutorotate返回以YES允许正确的方向...

Objc

- (BOOL)shouldAutorotate {

    [super shouldAutorotate];

    if (@available(iOS 11, *)) return YES;
    return NO;

}
Run Code Online (Sandbox Code Playgroud)

在组合中,我必须手动检查屏幕尺寸,以查看宽度是大于还是小于屏幕的假定高度.

width = self.view.frame.size.width, height = self.view.frame.size.height;
if (height < width) width = height, height = self.view.frame.size.width;
Run Code Online (Sandbox Code Playgroud)

希望其他人找到这个"错误"的真正原因或者 Apple更新他们的捆绑来处理像所有以前的iOS版本一样的旋转.

Swift 3.2+

override var shouldAutorotate: Bool {

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

}
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 11.2上-根本不会调用(BOOL)shouldAutorotate {}。不在根视图控制器中,也不在以后的视图控制器中。 (2认同)