我现在已经坚持了一段时间,并且无法弄清楚如何覆盖" shouldAutoRotate"来自子节点的TabBarController变量(Navigation Controller ---> TableViewController)
所以基本上就是我的设置 TabBarController --->导航控制器--->主TableViewController ---> VocabularyDetail TableviewController
我知道TabBarController中的下面覆盖将锁定所有子视图的旋转。
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override var shouldAutorotate : Bool {
return false
}
然而,挑战在于我希望有选择地执行此覆盖,具体取决于哪个视图已加载到导航控制器中。如果你看图片,最后一个控制器是" 词汇细节"哪一个应该改变" shouldAutorotate "变量为true。
答案 0 :(得分:0)
我终于明白了:) 对于那些陷入同样问题的人来说,这就是解决方案。
您需要编写两个扩展。一个用于TabBarController,一个用于NavigationController。然后在导航控制器的扩展中,您需要通过检查正在加载到导航中的视图来覆盖您感兴趣的值。
UITabBarController扩展基本上会传递来自子UINavigationController的值
extension UITabBarController {
override open var shouldAutorotate: Bool {
get {
if let visibleVC = selectedViewController {
return visibleVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let visibleVC = selectedViewController {
return visibleVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let visibleVC = selectedViewController {
return visibleVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}
}
UINavigation扩展将检查正在加载的视图并为覆盖设置正确的值。下面将基本上允许我的屏幕旋转,但对我感兴趣的方向。除了Flashcard之外的所有其他视图都将保留在纵向中,而闪存卡仅保留在横向中。
extension UINavigationController {
override open var shouldAutorotate: Bool {
get {
if let visibleVC = visibleViewController {
return visibleVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let visibleVC = visibleViewController {
if visibleVC.isKind(of: FlashCardController.classForCoder()) {
return UIInterfaceOrientation.landscapeLeft
} else {
return UIInterfaceOrientation.portrait
}
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let visibleVC = visibleViewController {
if visibleVC.isKind(of: FlashCardController.classForCoder()) {
return UIInterfaceOrientationMask.landscape
} else {
return UIInterfaceOrientationMask.portrait
}
}
return super.supportedInterfaceOrientations
}
}
}