我有一个应用程序,其中包含横向和纵向的所有视图,但是有两个视图控制器是严格的肖像。
我使用了以下方法,并且在除iPad之外的所有设备中都运行良好。
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
NSLog("Function called")
if isShouldRotate == true{
return UIInterfaceOrientationMask.AllButUpsideDown
}
return UIInterfaceOrientationMask.Portrait
}
然后我开始了解UINavigationController的子类,但不知道如何实现它。
请让我知道这些步骤。
答案 0 :(得分:1)
我通过继承UINavigationController类来解决它!
在App代表中:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
var currentViewController: UIViewController? = self.topViewController()
if currentViewController != nil && currentViewController!.canAutoRotate() {
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
func topViewController() -> UIViewController? {
if UIApplication.sharedApplication().keyWindow != nil
{
return self.topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!)
}
return nil
}
func topViewControllerWithRootViewController(rootViewController: UIViewController?) -> UIViewController? {
if rootViewController == nil {
return nil
}
if rootViewController!.isKindOfClass(UITabBarController) {
var tabBarController: UITabBarController = (rootViewController as? UITabBarController)!
return self.topViewControllerWithRootViewController(tabBarController.selectedViewController)
}
else {
if rootViewController!.isKindOfClass(UINavigationController) {
var navigationController: UINavigationController = (rootViewController as? UINavigationController)!
return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
}
else {
if (rootViewController!.presentedViewController != nil) {
var presentedViewController: UIViewController = rootViewController!.presentedViewController!
return self.topViewControllerWithRootViewController(presentedViewController)
}
else {
return rootViewController
}
}
}
}
在您不想旋转的特定视图控制器中:
override func canAutoRotate() -> Bool {
return false
}