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

时间:2016-01-19 19:14:23

标签: ios swift screen-orientation

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

    }

1 个答案:

答案 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
    }