Swift:为所有UINavigationControllers分配自定义类是否正确?

时间:2016-03-31 08:49:43

标签: xcode swift uinavigationcontroller uitabbarcontroller autorotate

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

enter image description here

我有一个根UITabBarController,有5个可能的选择。然后,我想要一些UIViewController可以旋转到横向,而我还希望其他一些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类分配给 CustomNavigationController,但我已将所有CustomTabBarController分配给同一个班级UITabBarController。 结果是没有视图控制器不旋转。这是因为我给他们分配了同一个班级吗?我应该为每个UINavigationController创建一个自定义导航控制器类吗?

更新

我发现的部分解决方案,即使它有点错综复杂,如下所示。我修改了以前的文件:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {

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

}

class CustomTabBarController:UITabBarController,UITabBarControllerDelegate {     override func shouldAutorotate() - >布尔{         return(self.selectedViewController as!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")
}

无论如何都有一点问题,因为将模式设置为肖像的动画不正确意味着不调整屏幕的宽度。如果我从横向视图控制器转到仅纵向视图控制器,则视图控制器框架不正确。我得到了

enter image description here

而不是:

enter image description here

1 个答案:

答案 0 :(得分:1)

在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中更改方向,该方向应仅在Portrait(HomeViewController,ServicesViewController,PhotoGalleryViewController,SelectMapViewController,MapViewController)中:

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