iOS 13中的自动旋转错误

Dee*_*rma 10 uiviewcontroller autorotate ios autolayout swift

iOS 13 / 13.1自动旋转的行为似乎与iOS 12不同。例如,我的应用程序允许用户在设置中将界面方向锁定为纵向或横向模式。

  1. 如果我在设备上设置了纵向旋转锁定并在supportedInterfaceOrientations中返回了.landscape,则该界面将保持纵向模式,直到我在设备上禁用了纵向锁定方向。iOS 12似乎并非如此。事实上,iOS 13中甚至没有调用supportedInterfaceOrientations!

  2. 在这种情况下,UIViewController.attemptRotationToDeviceOrientation()也不起作用。

问题的根源是我在应用初始化时以及在初始化所有内容时暂时将shouldAutorotate返回为false,我调用UIViewController.attemptRotationToDeviceOrientation()触发自动旋转。它在iOS 12中触发自动旋转,但在iOS 13.1中不起作用。

看起来像是iOS 13.1中的错误。我该如何强制触发自动旋转?

编辑:看起来iOS 12.4.1也忽略了UIViewController.attemptRotationToDeviceOrientation()。iOS 12.4.1及更高版本中的自动旋转功能有问题。

要清楚,这就是我想要的:

一种。即使在iPhone上设置了人像锁,我也希望我的界面在需要时能够自动旋转到横向模式,

b。UIViewController.attemptRotationToDeviceOrientation()替代方案,在所有情况下都会触发自动旋转。

Ami*_*n3t 0

尝试一下,看看这是否是您正在寻找的东西:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }

    @IBAction func btnLandscapeClicked(_ sender: Any) {
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }

    @IBAction func btnPortraitClicked(_ sender: Any) {
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }

}

extension UINavigationController {

    override open var shouldAutorotate: Bool {
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.shouldAutorotate
            }
            return super.shouldAutorotate
        }
    }

    override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.preferredInterfaceOrientationForPresentation
            }
            return super.preferredInterfaceOrientationForPresentation
        }
    }

    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.supportedInterfaceOrientations
            }
            return super.supportedInterfaceOrientations
        }
    }}
Run Code Online (Sandbox Code Playgroud)