Preventing rotation in a specific view controller in iOS 11

Hello,

I am trying to prevent rotation (lock it to say, portrait) in a specific VC that is
embedded in a navigation controller.

I am currently doing this:

To UINavigationController

extension UINavigationController {
public override func supportedInterfaceOrientations() → Int {
return visibleViewController.supportedInterfaceOrientations()
}
}

In my VC:

class ViewController: UIViewController {
override func supportedInterfaceOrientations() → Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
}

However, I have issues when we go to another VC (embedded in another nav controller) is presented which supports both landscape and portrait. Suppose, the user rotates in the new screen to landscape. And clicks back to go to original screen. The app is now presented in landscape as opposed to portrait defined in its supportedInterfaceOrientations override.

I read in iOS 11, we should use viewWillTransition(to:with:) to handle rotation (and locking as well).
Can you give directions on how to achieve it?

This is because in uiviewcontroller documentation
“As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransition(to:with:) method. When the interface orientation changes, UIKit calls this method on the window’s root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.”

@blueblood Do you still have issues or have you managed to solve this in the meantime?

This topic was automatically closed after 166 days. New replies are no longer allowed.