Search code examples
iosswiftuinavigationcontroller

Force change UINavigationController orientation


How can I force change orientation, when viewWillAppear?

I want only one navigationController to force landscape, other controllers I want to portrait.

When I push to landscape controller it shows like portrait, but when I rotate mobile, it locks, like landscape like I want.

How can I do that, when vieWillAppear? Is there any function to that?


Solution

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