Swift 3 kosher version
I left this here only in case anyone has a problem.
Apple's documentation for supportedInterfaceOrientations states:
When the user changes the orientation of the device, the system calls this method on the root view controller or the topmost view controller presented, which fills the window. If the view controller supports the new orientation, the window and the view controller rotate to the new orientation. This method is only called if the viewAutorotate method of the view controller returns true.
In a few words, you should override supportedInterfaceOrientations in the root view controller so that it returns a value for its top child view controller and default value otherwise.
What you have to do is check if the application supports all modes (go to the Deployment Information section in the general goals settings or Info.plist), find out the class of your root view controller. It can be a universal UIViewController, UINavigationController, UITabBarController or some custom class. You can check it as follows:
dump(UIApplication.shared.keyWindow?.rootViewController)
Or in any other way that you like.
Let it be some kind of CustomNavigationController . Therefore, you should override supportedInterfaceOrientations as follows:
class CustomNavigationController: UINavigationController { override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return topViewController?.supportedInterfaceOrientations ?? .allButUpsideDown } }
In any view controller that should only support portrait orientation, for example, override supportedInterfaceOrientations as follows:
class ChildViewController: UIViewController { override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } }
Then be sure to check if the shouldAutorotate controller shouldAutorotate and the topmost presented view controller should already return true . If not, add this to the class definitions:
override var shouldAutorotate: Bool { return true }
Otherwise, supportedInterfaceOrientations will not be called at all.
Here you go!
If you need to fix the opposite problem, when only one view controller needs to support several orientations, and the others do not, make these changes to each view controller, except for this.
Hope this helps.