【问题标题】:iOS 11 Device Orientation AutorotationiOS 11 设备方向自动旋转
【发布时间】:2017-09-22 19:51:19
【问题描述】:

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

对于我应该轮换的 VC,我有以下代码,我说过,在 iOS 11

之前可以运行
- (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 objective-c swift orientation ios11


    【解决方案1】:

    无论这是否被证明是一个 iOS 11 错误,我似乎偶然发现了一个“解决方案”来解决这个问题。无论出于何种原因,对于 iOS 11,将 - (BOOL)shouldAutorotate 返回更改为 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 版本一样处理轮换..

    斯威夫特 3.2+

    override var shouldAutorotate: Bool {
    
        if #available(iOS 11.0, *) {
            // Anything else iOS 11 specific
            return true
        } 
        return false
    
    }
    

    【讨论】:

    • 在 iOS 11.2 上 - (BOOL)shouldAutorotate {} 根本不被调用。不在根视图控制器中,也不在以后的视图控制器中。
    • 创建一个导航控制器并在你的 VC 没有调用时调用这个方法 - stackoverflow.com/questions/40413567/…
    • 我发现在我的UIApplicationDelegate 类中实现application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) 之前,iOS 11 不会调用这些方法中的任何一个。
    猜你喜欢
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多