我使用以下代码将视图控制器旋转到PortraitUpsideDown:
class VC : UIViewController {
override func shouldAutorotate() -> Bool {
let value = UIInterfaceOrientation.PortraitUpsideDown.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
return true
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue)
}
但是,在下一个视图控制器上,我希望屏幕方向返回到Portrait。我该如何重置?
这不起作用:
override func shouldAutorotate() -> Bool {
let value = UIInterfaceOrientation.PortraitUpsideDown.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
return false
}
答案 0 :(得分:1)
在viewDidAppear
:
override func viewDidAppear(animated: Bool) {
if UIDevice.currentDevice().orientation.isLandscape{
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
}
<强>更新强>
检查PortraitUpSideDown
if UIDevice.currentDevice().orientation.rawValue == UIInterfaceOrientation.PortraitUpsideDown.rawValue{
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
更新2
你想要PortaitupsideDown添加这段代码:
override func viewWillAppear(animated: Bool) {
if UIDevice.currentDevice().orientation.rawValue != UIInterfaceOrientation.PortraitUpsideDown.rawValue{
let value = UIInterfaceOrientation.PortraitUpsideDown.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.PortraitUpsideDown
}