我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
上
viewWillAppear
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何在viewWillAppear上旋转屏幕?
UPDATE
我使用下一个代码来锁定方向:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的第一个控制器viewWillAppear我设置:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)
在我的SECOND控制器中:
if let appDelegate = …Run Code Online (Sandbox Code Playgroud)