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

关于ios - swift :将屏幕方向重置为纵向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34885127/

10-12 05:33