Xcode Swift:将自定义类分配给所有UINavigationController是否正确?
这或多或少是我的Xcode Swift:将自定义类分配给所有UINavigationController是否正确?,xcode,swift,uinavigationcontroller,uitabbarcontroller,autorotate,Xcode,Swift,Uinavigationcontroller,Uitabbarcontroller,Autorotate,这或多或少是我的Main.storyboard情况: 其中我有一个rootUITabBarController,有5个可能的选择。然后,我希望一些UIViewControllers可以旋转到横向,而我还希望一些其他UIViewControllers只有横向模式。所以我写了这个文件。swift: class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { overrid
Main.storyboard
情况:
其中我有一个rootUITabBarController
,有5个可能的选择。然后,我希望一些UIViewController
s可以旋转到横向,而我还希望一些其他UIViewController
s只有横向模式。所以我写了这个文件。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
s分配了相同的类
CustomNavigationController
而我已将CustomTabBarController
类分配给UITabBarController
。
结果是没有视图控制器不旋转。这是因为我给他们分配了相同的课程吗?我应该为我拥有的每个UINavigationController
创建一个自定义导航控制器类吗
更新
我发现的一个部分解决方案,即使有点复杂,如下所示。我修改了前面的文件,如下所示:
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
override func shouldAutorotate() -> Bool {
return (self.topViewController?.shouldAutorotate())!
}
}
类CustomTabBarController:UITabBarController,UITabBarController Delegate{
重写func shouldAutorotate()->Bool{
return(self.selectedViewController作为!UINavigationController)。shouldAutorotate()
}
}
然后,在允许旋转的视图控制器中,我只需要:
override func shouldAutorotate() -> Bool {
return true
}
在不允许旋转的视图控制器中我有:
override func shouldAutorotate() -> Bool {
return false
}
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
无论如何,有一个小问题,因为设置模式为纵向的动画是不正确的,这意味着屏幕的宽度没有调整。如果我从横向视图控制器转到仅纵向视图控制器,则视图控制器框架不正确。我明白了
与此相反:
在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):
谢谢你的建议,但我担心它不起作用……它们都会轮换,毫无例外:(@LoryLory抱歉,我编辑了我的答案。以前的答案只适用于演示的控制器。这个应该可以。而且,即使没有CustomNavigationController,它也可以工作。试试看;)好的,它可以工作,但现在我的问题更新中描述了问题:如果我从横向视图控制器转到仅纵向视图控制器,则视图控制器框架不正确。使用新图像。@Lory为此,您需要在HomeViewController、ServicesViewController、PhotoGalleryViewController、SelectMapViewController、MapViewController中再添加一行代码。查看答案您编写的代码与我在ViewWillAppease()中添加的代码完全相同,但“转换”不正确……我得到的strage图像就像表视图保持在横向模式一样。我也尝试过更新它的框架,但id没有改变
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}