How to disable automatic rotation for specific view controllers inside a navigation controller (Swift)?

^ There is no answer above at all ... Problems with disabling automatic rotation for certain (not all) view controllers that are inside the navigation controller. Such questions do not concern the ability to disable autorotation for certain view controllers, but rather disable autorotation in all view controllers inside the 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 satisfactorily answer this.

+4
source share
1 answer

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

Although @Sidetalker's answer is correct, I think he lacks a bit of explanation.

Basically you create a custom class for UINavigationControllerand assign it UINavigationControllerto the Storyboard. In the user class, UINavigationControlleryou overrideuse the function shouldAutorotateand check if there is topViewController ViewController(the class of yours UIViewControllerin the Storyboard) the class on which you want to disable autorotation.

In user 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
}
+4
source

Source: https://habr.com/ru/post/1618801/


All Articles