2

I know if VC has navigationController it should contain some like this. But what about swift? How to able call shouldAutoRotate in VC with navigationController? My navigationControllers has standard class UINavigationController (not custom). I want make shouldAutoRotate false for all my VC except single. As I understood there no another ways to just use "Portrait" and make available to rotate single VC

Community
  • 1
  • 1
Max
  • 1,341
  • 3
  • 20
  • 39

4 Answers4

3

Swift 4

The most effective way I found is to add this extension on a given UIViewController - one that first comes into the view:

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
        }
    }

}

Then you need to override the shouldAutorotate function. Add this to your UIViewController class:

override open var shouldAutorotate: Bool {
    return false
}

Any UIViewController you navigate to, you can just override the shouldAutorotate function and set it to whatever you prefer (return true or false).

For any views that appear modally, override supportedInterfaceOrientations:

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
}
Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
2

Put this in your view controllers:

override func shouldAutorotate() -> Bool {

     return false // or true for the VC that allows landscape
}
SirNod
  • 781
  • 6
  • 10
  • 1
    `override func shouldAutorotate() -> Bool` gives: Method does not override any method from its superclass. And removing override gives me: Method 'shouldAutorotate()' with Objective-C selector 'shouldAutorotate' conflicts with getter for 'shouldAutorotate' from superclass 'UIViewController' with the same Objective-C selector – Jorge Irún Nov 10 '16 at 16:52
2

Swift 3 needs the type to be var and not func:

override var shouldAutorotate: Bool {
    return false
}
D Lindsey
  • 111
  • 6
0

Works on iOS13.4 simulator -

import UIKit

var autorotateEnabled: Bool = true

public class MainViewController : UIViewController {

    @IBAction func lockAutoRotate(_ sender: Any) {
        autorotateEnabled = !autorotateEnabled
    }
    
    @IBAction func SetOrientationTest(_ v: UIView){
        // Change the desired orientation of this activity.
        if UIDevice.current.orientation == .portrait {
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        } else {
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        }
    }
    
    override public func viewDidLoad() {
        var savedInstanceState = UserDefaults.standard.dictionaryRepresentation()
        super.viewDidLoad()
    }
}

extension UINavigationController {
    override open var shouldAutorotate: Bool {
       return autorotateEnabled
    }
}
ReflectCode
  • 111
  • 2
  • 4