使用Swift以编程方式启用和禁用自动旋转?

Geo*_*Lee 1 iphone ios swift ios9

我想使用Swift按钮以编程方式禁用或启用自动旋转功能.我当时认为它可以以某种方式使用该supportedInterfaceOrientations()功能完成,但在查看有关如何完成的文献后我感到非常困惑.有一个简单的解决方案吗?

有人可以给我建议或起点吗?

Snu*_*ken 5

您可以为按钮创建一个操作,在代码中的某处设置一个布尔标志,并shouldAutorotate在视图控制器的方法中返回该标志的值.如果您需要为所有视图控制器,您可以创建一个公共基本视图控制器(继承).

按钮操作示例:

@IBAction func toggleRotation(sender: Button) {
    // A made up AppConfig class with class method for setting and retrieving 
    // rotation flag.
    AppConfig.allowRotation(!AppConfig.allowRotation)
}
Run Code Online (Sandbox Code Playgroud)

shouldAutorotate的示例:

override func shouldAutorotate() -> Bool {
    return AppConfig.allowRotation()
}
Run Code Online (Sandbox Code Playgroud)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/shouldAutorotate