How to force view controller orientation in iOS 8?

前端 未结 25 2385
太阳男子
太阳男子 2020-11-22 13:11

Before iOS 8, we used below code in conjunction with supportedInterfaceOrientations and shouldAutoRotate delegate methods to force app orie

25条回答
  •  耶瑟儿~
    2020-11-22 13:30

    The combination of Sids and Koreys answers worked for me.

    Extending the Navigation Controller:

    extension UINavigationController {
        public override func shouldAutorotate() -> Bool {
            return visibleViewController.shouldAutorotate()
        }
    }
    

    Then disabling rotation on the single View

    class ViewController: UIViewController {
        override func shouldAutorotate() -> Bool {
            return false
        }
    }
    

    And rotating to the appropriate orientation before the segue

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "SomeSegue")
        {
            let value = UIInterfaceOrientation.Portrait.rawValue;
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }
    }
    

提交回复
热议问题