Extending the answer of Josh Gaffney and user3655266, this concept also extends to view controllers.
If we have a UIViewController that is a child of another UIViewController in the view hierarchy, overriding the shouldAutorotate () child method to false can still rotate, since its parent controller can still return true . It is also important to know that although the child VC is displayed, the parent function shouldAutoRotate is still called. Therefore, control must lie there.
Swift 5
class ParentViewController:UIViewController{ override func shouldAutorotate() -> Bool {
** The same can be done to allow only a specific orientation of the phone **
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { // This function is called on the parent controller whenever the child of this parent is trying to rotate let childrenVCArray = self.children if childrenVCArray.count > 0 { // assuming the array of the first element is the current childVC let topMostVC = childrenVCArray[0] if topMostVC is ChildViewController { // Assuming only allowing landscape mode return .landscape } } // Return portrait otherwise return .portrait }
source share