# Preventing rotation in a specific view controller in iOS 11

**URL:** <https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319>\
**Category:** iOS\
**Created:** [November 13, 2018, 6:18am UTC](https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319 "2018-11-13T06:18:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueblood](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@blueblood](https://forums.kodeco.com/u/blueblood)\
**Post date:** [November 13, 2018, 6:18am UTC](https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319/1 "2018-11-13T06:18:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![blueblood](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@blueblood](https://forums.kodeco.com/u/blueblood)\
**Post date:** [November 13, 2018, 6:20am UTC](https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319/2 "2018-11-13T06:20:12Z")

</div>

This is because in [uiviewcontroller documentation](https://developer.apple.com/documentation/uikit/uiviewcontroller)  
“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:)`](https://developer.apple.com/documentation/uikit/uicontentcontainer/1621466-viewwilltransition) 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.”

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [December 12, 2018, 8:45pm UTC](https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319/3 "2018-12-12T20:45:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 28, 2019, 10:28pm UTC](https://forums.kodeco.com/t/preventing-rotation-in-a-specific-view-controller-in-ios-11/55319/4 "2019-04-28T22:28:37Z")

</div>

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