Force landscape mode in one ViewController using Swift

后端 未结 19 1200
情书的邮戳
情书的邮戳 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:08

    Swift 4

    override func viewDidLoad() {
        super.viewDidLoad()
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
    

    If your view is embedded in a navigation controller, the above alone won't work. You have to cascade up by the following extension after the class definition.

    extension UINavigationController {
    
    override open var shouldAutorotate: Bool {
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.shouldAutorotate
            }
            return super.shouldAutorotate
        }
    }
    
    override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.preferredInterfaceOrientationForPresentation
            }
            return super.preferredInterfaceOrientationForPresentation
        }
    }
    
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get {
            if let visibleVC = visibleViewController {
                return visibleVC.supportedInterfaceOrientations
            }
            return super.supportedInterfaceOrientations
        }
    }}
    


    Swift 3

    override func viewDidLoad() {
        super.viewDidLoad()
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }
    
    private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.landscapeLeft
    }
    
    private func shouldAutorotate() -> Bool {
        return true
    }
    
    0 讨论(0)
  • 2020-12-02 08:11
    // below code put in view controller
    // you can change landscapeLeft or portrait
    
    override func viewWillAppear(_ animated: Bool) {
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        }
    
    override var shouldAutorotate: Bool {
            return true
        }
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .landscapeRight
        }
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .landscapeRight
        }
    
    0 讨论(0)
  • 2020-12-02 08:11
    class CustomUIViewController : UIViewController{
    
        override var   supportedInterfaceOrientations : UIInterfaceOrientationMask{
    
            return  .landscapeLeft
    
        }
    
    }
    
    
    class ViewController: CustomUIViewController {
    .
    .
    .
    }
    
    0 讨论(0)
  • 2020-12-02 08:12

    For me, the best results came from combining Zeesha's answer and sazz's answer.

    Add the following lines to AppDelegate.swift:

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

    Add the following line to your view controller class:

    let appDel = UIApplication.shared.delegate as! AppDelegate
    

    Add the following lines to your view controller's viewDidLoad():

    appDel.myOrientation = .landscape
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
    

    (optional) Add this line to your dismiss function:

    appDel.myOrientation = .portrait
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
    

    What these lines of code do is set the default orientation to portrait, rotate it landscape when the view controller loads, and then finally reset it back to portrait once the view controller closes.

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

    Works in Swift 2.2

     func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if self.window?.rootViewController?.presentedViewController is SignatureViewController {
    
            let secondController = self.window!.rootViewController!.presentedViewController as! SignatureViewController
    
            if secondController.isPresented {
    
                return UIInterfaceOrientationMask.LandscapeLeft;
    
            } else {
    
                return UIInterfaceOrientationMask.Portrait;
            }
    
        } else {
    
            return UIInterfaceOrientationMask.Portrait;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 08:13

    My solution is

    just added below codes in AppDelegate

    enum PossibleOrientations {
      case portrait
        case landscape
    
        func o() -> UIInterfaceOrientationMask {
          switch self {
          case .portrait:
            return .portrait
          case .landscape:
            return .landscapeRight
          }
        }
    }
    var orientation: UIInterfaceOrientationMask = .portrait
    
    func switchOrientation(to: PossibleOrientations) {
        let keyOrientation = "orientation"
    
        if to == .portrait && UIDevice.current.orientation.isPortrait {
            return
        } else if to == .landscape && UIDevice.current.orientation.isLandscape {
            return
        }
    
        switch to {
        case .portrait:
            orientation = .portrait
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: keyOrientation)
        case .landscape:
            orientation = .landscapeRight
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: keyOrientation)
        }
    }
    

    And call below codes to change

    override func viewDidLoad() {
        super.viewDidLoad()
    
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.switchOrientation(to: .landscape)
        }
    }
    

    or like below

    @IBAction func actBack() {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.switchOrientation(to: .portrait)
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题