横向视图控制器应用程序委托 [英] Landscape orientation View Controller App Delegate

查看:31
本文介绍了横向视图控制器应用程序委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅将一个视图控制器置于横向模式,而将其他控制器置于纵向模式.

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)
}

这篇关于横向视图控制器应用程序委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆