我想通过使用Swift的按钮以编程方式禁用或启用自动旋转功能。我当时以为可以使用该supportedInterfaceOrientations()
函数完成此操作,但是在查阅有关如何完成此功能的文献后,我感到非常困惑。有一个简单的解决方案吗?
您可以为按钮创建一个操作,该操作在代码中的某个位置设置布尔标志,并shouldAutorotate
在视图控制器的方法中返回该标志的值。如果需要所有视图控制器,则可以创建一个通用的基本视图控制器(继承)。
按钮动作示例:
@IBAction func toggleRotation(sender: Button) {
// A made up AppConfig class with class method for setting and retrieving
// rotation flag.
AppConfig.allowRotation(!AppConfig.allowRotation)
}
shouldAutorotate的示例:
override func shouldAutorotate() -> Bool {
return AppConfig.allowRotation()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句