如何禁用导航控制器 (Swift) 内特定视图控制器的自动旋转? [英] How to disable auto-rotation for specific View Controllers inside a Navigation Controller (Swift)?
问题描述
^Above 根本没有答案......在导航控制器内的特定(并非所有)视图控制器禁用自动旋转时遇到问题.类似的问题没有解决为特定视图控制器禁用自动旋转的能力,而是在导航控制器内的所有视图控制器中禁用自动旋转.我的导航控制器包含一些我想要自动旋转的 VC,以及我不想自动旋转的其他 VC.没有现有问题可以令人满意地回答这个问题.
^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.
推荐答案
我做了一个关于如何做到这一点的示例项目:GitHub 存储库.
I made an example project on how to do this: GitHub repo.
虽然@Sidetalker 的回答是正确的,但我认为它缺乏一些解释.
While @Sidetalker's answer is correct I think it lacks a bit of explanation.
基本上,您为 UINavigationController
创建一个自定义类并将其分配给 Storyboard 中的 UINavigationController
.在自定义 UINavigationController
类中,您 override
shouldAutorotate
函数并检查 topViewController
是否为 ViewController代码>(故事板中
UIViewController
的类)要在其上禁用自动旋转的类.
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.
在自定义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
}
这篇关于如何禁用导航控制器 (Swift) 内特定视图控制器的自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!