gpt4 book ai didi

xcode - Swift:将自定义类分配给所有 UINavigationController 是否正确?

转载 作者:可可西里 更新时间:2023-11-01 02:17:15 25 4
gpt4 key购买 nike

这或多或少是我的 Main.storyboard 情况:

enter image description here

我的根 UITabBarController 有 5 个可能的选择。然后,我希望某些 UIViewController 可以旋转到横向,而我还希望其他一些 UIViewController 仅具有横向模式。所以我写了这个 file.swift:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {

override func shouldAutorotate() -> Bool {
if (self.topViewController?.isKindOfClass(HomeViewController) != nil) {return false}
else if (self.topViewController?.isKindOfClass(ServicesViewController) != nil) {return false}
else if (self.topViewController?.isKindOfClass(PhotoGalleryViewController) != nil) {return false}
else if (self.topViewController?.isKindOfClass(SelectMapViewController) != nil) {return false}
else if (self.topViewController?.isKindOfClass(MapViewController) != nil) {return false}
else {return true}
}

}

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
override func shouldAutorotate() -> Bool {
return (self.selectedViewController as! UINavigationController).shouldAutorotate()
}
}

并且我已经为所有 UINavigationController 分配了同一个类
CustomNavigationController
,而我已将 CustomTabBarController 类分配给 UITabBarController。结果是没有 View Controller 不旋转。这是因为我给他们分配了同一个类(class)吗?我应该为我拥有的每个 UINavigationController 创建一个自定义导航 Controller 类吗?

更新

我找到的部分解决方案如下,即使它有点复杂。我已经像这样修改了以前的文件:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {

override func shouldAutorotate() -> Bool {
return (self.topViewController?.shouldAutorotate())!
}

}

类 CustomTabBarController: UITabBarController, UITabBarControllerDelegate { 覆盖 func shouldAutorotate() -> Bool { 返回(self.selectedViewController as!UINavigationController).shouldAutorotate() }

然后,在允许旋转的 View Controller 中,我只需:

override func shouldAutorotate() -> Bool {
return true
}

不允许旋转的 View Controller 中,我有:

override func shouldAutorotate() -> Bool {
return false
}

override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

无论如何有一个小问题,因为将模式设置为纵向的动画不正确,这意味着屏幕的宽度没有调整。如果我从横向 View Controller 转到仅纵向 View Controller ,则 View Controller 框架不正确。我明白了

enter image description here

而不是这个:

enter image description here

最佳答案

在 AppDelegate 中试试这个

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootController = self.window?.rootViewController as? UITabBarController {
if let navigationController = rootController.selectedViewController as? UINavigationController {
let controller = navigationController.topViewController!

if controller.isKindOfClass(HomeViewController.classForCoder()) {
return UIInterfaceOrientationMask.Portrait
}
if controller.isKindOfClass(ServicesViewController.classForCoder()) {
return UIInterfaceOrientationMask.Portrait
}
if controller.isKindOfClass(PhotoGalleryViewController.classForCoder()) {
return UIInterfaceOrientationMask.Portrait
}
if controller.isKindOfClass(SelectMapViewController.classForCoder()) {
return UIInterfaceOrientationMask.Portrait
}
if controller.isKindOfClass(MapViewController.classForCoder()) {
return UIInterfaceOrientationMask.Portrait
}
}
}
return UIInterfaceOrientationMask.All
}

更新:

此方法强制应用程序更改 ViewController 中的方向,该方向应仅在纵向(HomeViewController、ServicesViewController、PhotoGalleryViewController、SelectMapViewController、MapViewController)中:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

关于xcode - Swift:将自定义类分配给所有 UINavigationController 是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com