我尝试了下一步:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
关于
viewWillAppear
但它没有效果。在viewWillAppear中如何旋转我的屏幕?
更新
我使用下面的代码来锁定方向:
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
}
Swift 4.0
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
在我的第一个控制器中的viewWillAppear函数中,我设置了:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
在我的第二个控制器中:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}
所以,当我从第二个控制器回到第一个控制器时:
我将其向左旋转 - 没有旋转,向右旋转 - 什么都没有发生,再次向左旋转 - 它才开始旋转。
我该如何修复它?