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

心不动则不痛 提交于 2019-12-22 05:59:54

问题


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


回答1:


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
}


来源:https://stackoverflow.com/questions/34120574/how-to-disable-auto-rotation-for-specific-view-controllers-inside-a-navigation-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!