横向视图控制器应用程序委托
我试图仅将一个视图控制器置于横向模式,而将其他控制器置于纵向模式.
I'm trying to put only a View Controller in Landscape mode and the other ones in Portrait mode.
首先,我试图使每个视图控制器的旋转无效,除了我想要的那个(使用 shouldAutoRotate false,只有 Portrait,...),但是我有一个导航控制器,它与导航重叠bar 与状态栏,我无法解决它.所以在那之后,我尝试清理我所做的一切,并仅从 AppDelegate 启用或禁用方向.
First of all, I've tried to invalidate the rotation of each View Controller except for the one I want (With shouldAutoRotate false, only Portrait, ...), but with a Navigation Controller I have, it overlaps the Nav bar with the Status Bar, and I couldn't solve it. So after that, I've tried to clean everything I've done and enable or disable the Orientation ONLY from the AppDelegate.
所以这是我找到并尝试过的代码:
So here's a code I found and tried:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if self.window?.rootViewController?.presentedViewController is SecondViewController {
let secondController = self.window!.rootViewController!.presentedViewController as! SecondViewController
if secondController.isPresented {
return Int(UIInterfaceOrientationMask.All.rawValue);
} else {
return Int(UIInterfaceOrientationMask.Portrait.rawValue);
}
} else {
return Int(UIInterfaceOrientationMask.Portrait.rawValue);
}
}
但是这段代码永远不会出现在第一个 if(即使视图控制器是 SecondViewController).
But this code never goes in the first if (even if the View controller is SecondViewController).
所以知道如何检查我是否在可以指定方向的特定 VC 中吗?
So any idea of how I can check if I'm in a specific VC that I can specify the orientation?
提前致谢!
查看这个递归获取当前 viewController 的方法
Checkout this recursive method for getting current viewController
func getCurrentViewController(viewController:UIViewController?)-> UIViewController?{
if let tabBarController = viewController as? UITabBarController{
return getCurrentViewController(tabBarController.selectedViewController)
}
if let navigationController = viewController as? UINavigationController{
return getCurrentViewController(navigationController.visibleViewController)
}
if let viewController = viewController?.presentedViewController {
return getCurrentViewController(viewController)
}else{
return viewController
}
}
并在支持的方向方法上使用它,如下所示
And use it on supported orientation method as follow
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if let currentVC = getCurrentViewController(self.window?.rootViewController) as? SecondViewController{
return Int(UIInterfaceOrientationMask.All.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}