Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable auto-rotation for specific View Controllers inside a Navigation Controller (Swift)? [duplicate]

^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.

like image 564
IvOS Avatar asked Sep 26 '22 00:09

IvOS


1 Answers

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
}
like image 158
hungrxyz Avatar answered Oct 13 '22 12:10

hungrxyz