Force landscape mode in one ViewController using Swift

后端 未结 19 1258
情书的邮戳
情书的邮戳 2020-12-02 07:44

I am trying to force only one view in my application on landscape mode, I am calling

override func shouldAutorotate() -> Bool {
    print(\"shouldAutoro         


        
19条回答
  •  隐瞒了意图╮
    2020-12-02 08:06

    In AppDelegate add this:

    //Orientation Variables
    var myOrientation: UIInterfaceOrientationMask = .portrait
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return myOrientation  
    }
    

    Add this in viewController, that want to change orientation:

    override func viewDidLoad() {
            super.viewDidLoad()
            self.rotateToLandsScapeDevice()
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            self.rotateToPotraitScapeDevice()
        }
    
        func rotateToLandsScapeDevice(){
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.myOrientation = .landscapeLeft
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
            UIView.setAnimationsEnabled(true)
        }
    
        func rotateToPotraitScapeDevice(){
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.myOrientation = .portrait
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
            UIView.setAnimationsEnabled(true)
        }
    

提交回复
热议问题