swift:将屏幕方向重置为纵向

swift: reset screen orientation to portrait

我使用以下代码将视图控制器旋转到 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)
    }

但是,在下一个视图控制器上,我希望屏幕方向回到纵向。我该如何重置它?

这没有用:

override func shouldAutorotate() -> Bool {

        let value = UIInterfaceOrientation.PortraitUpsideDown.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
        return false

    }

在你的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
    }