I have an application with several VCs built into the NavigationController.
I need all VCs to be locked in portrait orientation and only one in portrait and landscape.
I tried different approaches and this one works for me:
extension UINavigationController { public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations() } public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } }
And then I modify each ViewController as follows:
class ViewController: UIViewController { override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) } }
Everything is fine when I open the application and move from one VC to another. Representations, which should be only "Portrait", are only "Portrait". Then I open a view that has not been changed, and it changes the Orientation interface, as intended.
BUT , then let me say that I leave the last unmodified view in the Landscape, and I press the button back in the NavBar and go to the previous controller (which was changed only for Portrait). The VC layout is shown as Landscape.
And if I continue to press the "Back" button, all other VCs behave as if they were never told only about the portrait.
I used this post Corey Hinton, Screen rotation lock
source share