无法强制 UIViewController 方向 [英] Unable to force UIViewController orientation

查看:24
本文介绍了无法强制 UIViewController 方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个横向 ViewController 的纵向应用程序.

I have a portrait application with one landscape ViewController.

当应用程序锁定为纵向时,我一直在研究如何强制横向方向,并且我尝试了此处提供的许多解决方案,但到目前为止还没有任何运气.

I've been digging a lot on how to force the orientation to landscape when the app is locked to portrait and I tried a lot of solutions presented here and not only but without any luck so far.

到目前为止,我设法自动旋转了横向所需的 VC,但由于方向未锁定,所有其他 VC 也将旋转.

So far I managed to autorotate the VC I need in landscape but since the orientation is not locked, all other VCs will also rotate.

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

override func shouldAutorotate() -> Bool
{
    return true
}

经过更多的挖掘,我在找到一个示例项目后最终得到了这个,但是虽然它在那个项目中有效,但它似乎对我不起作用.

After some more digging I ended up with this after finding a sample project but while it works in that project, it doesn't seem to work for me.

这是在 AppDelegate 中

This is in AppDelegate

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

    if self.window?.rootViewController?.presentedViewController is ConclusionReferencesVC {

        let conclusionReferencesVC = self.window!.rootViewController!.presentedViewController as! ConclusionReferencesVC

        if conclusionReferencesVC.isPresented
        {
            return UIInterfaceOrientationMask.LandscapeRight;
        }
        else
        {
            return UIInterfaceOrientationMask.Portrait;
        }
    }
    else
    {
        return UIInterfaceOrientationMask.Portrait;
    }
}

这是我想要横向发展的 VC:

This is in the VC I want to have in landscape:

var isPresented = true

@IBAction
func dismiss()
{
    isPresented = false
    self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil);
}

由于某种原因,supportedInterfaceOrientationsForWindow 方法没有验证初始条件.

For some reason, the supportedInterfaceOrientationsForWindow method does not validate the initial condition.

我也尝试过这些,但没有运气:

I also tried these among others but no luck:

如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式

Swift 2.0 中支持的InterfaceOrientationsForWindow

有什么想法吗?似乎我遗漏了一些东西,但我不知道是什么.

Any ideas? It seems I'm missing something but I can't figure it out what.

推荐答案

试试这个强制LandscapeRight模式

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
        {
            let value = UIInterfaceOrientation.LandscapeRight.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }

然后使用这样的类别

    import UIKit

extension UINavigationController{

    override public func shouldAutorotate() -> Bool
    {
    return (self.viewControllers.last?.shouldAutorotate())!
    }

    override public func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
    {
    return (self.viewControllers.last?.supportedInterfaceOrientations())!;
    }

    override public func preferredInterfaceOrientationForPresentation()-> UIInterfaceOrientation
    {
    return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation())!;
    }

}

已编辑

如果您不使用导航控制器,请使用此

EDITED

If you are not using navigation controller use this

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
        {
            let value = UIInterfaceOrientation.LandscapeRight.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }

    override func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
    {
        return .LandscapeRight;
    }

希望对你有帮助

这篇关于无法强制 UIViewController 方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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