如何仅解锁一个 Controller 的方向?我的整个应用程序只允许使用一种纵向模式,但仅在一种模式下我需要横向模式。为此,我尝试了下一个片段:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
但这对我没有帮助。还有其他解决办法吗?
最佳答案
在Appdelegate中定义一个var:
import UIKit
var isViewAppeared = false
现在在 Appdelegate 中:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if isViewAppeared{
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
现在在 ViewController
中您想要横向的位置:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
isViewAppeared = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
isViewAppeared = false
UIView.animateWithDuration(0.4, animations: { () -> Void in
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
})
}
关于ios - 如何只为一个 Controller 解锁方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331277/