横向定位View Controller应用程序委托 [英] Landscape orientation View Controller App Delegate

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

问题描述

我试图仅将View Controller置于横向"模式,将其他控制器置于纵向"模式.

I'm trying to put only a View Controller in Landscape mode and the other ones in Portrait mode.

首先,我尝试使每个View Controller的旋转无效,除了我想要的旋转(使用shouldAutoRotate false,只有Portrait,...),但是对于我拥有的Navigation Controller,它与Nav重叠状态栏显示,我无法解决.因此,在此之后,我尝试清除已完成的所有操作,并仅从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);
        }

    }

但是,即使View控制器是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)
}

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

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