简体   繁体   English

iOS Swift UIViewController旋转

[英]iOS Swift UIViewController Rotation

I have a UITabViewController with three tabs. 我有一个带有三个选项卡的UITabViewController。 On one of the tabs, the user taps a button and a new UIViewController controller pops up in Landscape mode (for the user to sign their name using their finger). 在其中一个选项卡上,用户点击一个按钮,然后在“风景”模式下弹出一个新的UIViewController控制器(供用户使用自己的手指签名)。 After the user is finished, they tap a Finish button and finish() is called which closes the signing UIViewController. 用户完成操作后,他们点击“完成”按钮,然后调用finish()来关闭签名UIViewController。

The problem I am facing is the screen that the user returns to that was in Portrait mode is now in Landscape mode even though the screen is set: 我面临的问题是即使设置了屏幕,用户返回的屏幕仍以“纵向”模式现在处于“横向”模式:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    return .portrait
}

override var shouldAutorotate: Bool {
    return true
}

and there nothing I can do to force rotation back to portrait. 我无能为力地将旋转恢复为纵向。 I have read posts such as: How to force device rotation in Swift? 我已经阅读了以下文章: 如何在Swift中强制设备旋转? and as I understand it, there is nothing I can do to rotate the screen back to portrait? 而且据我了解,我无法将屏幕旋转回纵向吗?

It's really simple. 真的很简单。 Write the following code and change orientation value in ViewControllers you want to programmatically rotate. 编写以下代码,并在要以编程方式旋转的ViewController中更改方向值。

override func viewWillAppear(_ animated: Bool) {
        let value = UIInterfaceOrientation.portrait.rawValue //Change orientation according to your scenario
        UIDevice.current.setValue(value, forKey: "orientation")
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM