Force landscape mode in one ViewController using Swift

后端 未结 19 1197
情书的邮戳
情书的邮戳 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 07:54

    In ViewController in viewDidLoad Method call below function

    func rotateDevice(){
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true) // while rotating device it will perform the rotation animation
    }`
    

    App Delegate File Add Below Function & Variables

    //Orientation Variables
    var orientationLock = UIInterfaceOrientationMask.portrait
    var myOrientation: UIInterfaceOrientationMask = .portrait
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return .landscape }
    
    0 讨论(0)
  • 2020-12-02 07:55

    I needed to force one controller into portrait orientation. Adding this worked for me.

    swift 4 with iOS 11

    override var   supportedInterfaceOrientations : UIInterfaceOrientationMask{
    
        return  .portrait
    
    }
    
    0 讨论(0)
  • 2020-12-02 07:58

    It may be useful for others, I found a way to force the view to launch in landscape mode:

    Put this in the viewDidLoad():

    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    

    and,

    override var shouldAutorotate: Bool {
        return true
    }
    
    0 讨论(0)
  • 2020-12-02 07:59

    According to the documentation of supportedInterfaceOrientations the shouldAutorotate method should return true or YES in Objective-C so that the supportedInterfaceOrientations are considered.

    0 讨论(0)
  • 2020-12-02 08:00

    Swift 4 , Tested in iOS 11

    You can specify the orientation in projectTarget -> General -> DeploymentInfo(Device Orientation) -> Portrait (Landscapeleft and Landscaperight are optional)

    AppDelegate

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

    LandScpaeViewController

    override func viewDidLoad() {
            super.viewDidLoad()
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.myOrientation = .landscape
    }
    

    OnDismissButtonTap

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
     appDelegate.myOrientation = .portrait
    

    Thats it. :)

    0 讨论(0)
  • 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)
        }
    
    0 讨论(0)
提交回复
热议问题