我的应用程序有许多不同的ViewController,我想允许某些特定的ViewController可以旋转横向。我尝试过使用supportedInterfaceOrientations或shouldAutorotate之类的方法,但它不起作用。 还有人有其他想法吗?
最佳答案
仅在 ViewController 中更新是不够的,您必须在 AppDelegate 中更新更多内容。
应用程序委托(delegate)
var enableAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if enableAllOrientation {
return .all
}
return .portrait
}
任何你想要支持景观的ViewController,只需实现:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.enableAllOrientation = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.enableAllOrientation = false
}
关于ios - 只在某些特定的 ViewController 中启用横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334313/