5

^Above does not have answer at all...Having issues disabling auto rotation for specific (not all) View Controllers that are inside a navigation controller. Similar questions do not address the ability to disable autorotation for specific view controllers but rather to disable autorotation in all of the view controllers inside of a navigation controller. My navigation controller contains some VCs that I would like to have autorotation and others that I do not want to autorotate. No existing questions answer this satisfactorily.

IvOS
  • 323
  • 3
  • 13
  • The answer suggested does not offer swift code and any similar questions do not provide working code for the new version of swift. – IvOS Dec 06 '15 at 20:07
  • Adding `shouldAutorotate()` to the `navigationController` instead of the `viewController` fixed it for me. Make sure to assign a class to the `navigationController`. I can elaborate if you want me to. – StevoHN Dec 07 '15 at 07:26
  • I have an idea of what you are referring to but would greatly appreciate if you could elaborate...thanks in advance @StevoHN – IvOS Dec 07 '15 at 17:39

1 Answers1

4

I made an example project on how to do this: GitHub repo.

While @Sidetalker's answer is correct I think it lacks a bit of explanation.

Basically you create a Custom Class for your UINavigationController and assign it to UINavigationController in Storyboard. In the custom UINavigationController class you override the shouldAutorotate function and check if the topViewController is ViewController(the class of your UIViewController in Storyboard) of the class on which you want to disable autorotate.

In custom UINavigationController:

override func shouldAutorotate() -> Bool {
if !viewControllers.isEmpty {

  // Check if this ViewController is the one you want to disable roration on
  if topViewController!.isKindOfClass(ViewController) {

    // If true return false to disable it
    return false
  }
}

// Else normal rotation enabled
return true
}
hungrxyz
  • 745
  • 11
  • 20