无法强制UIViewController方向

3
我有一个肖像应用程序,其中一个横向的ViewController。
我一直在研究如何在应用程序锁定为肖像模式时强制将方向设置为横向,并尝试了很多这里以及其他地方提出的解决方案,但到目前为止都没有成功。
到目前为止,我成功地使需要横向旋转的VC自动旋转,但由于方向未被锁定,所有其他VC也会旋转。
override func viewDidAppear(animated: Bool)
{
    let value = UIInterfaceOrientation.LandscapeRight.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

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

经过一番深入挖掘,我在找到一个示例项目后得出了这个结果,但虽然它在那个项目中可以工作,但对我来说似乎无法工作。

这是在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:

var isPresented = true

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

由于某种原因,supportedInterfaceOrientationsForWindow方法无法验证初始条件。

我还尝试了其他方法,但都没有成功:

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

Swift 2.0中的supportedInterfaceOrientationsForWindow

有什么想法吗?似乎我漏掉了什么,但我想不出来是什么。

3个回答

3

尝试使用以下代码强制将设备仅限于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())!;
    }

}

编辑后

如果您未使用导航控制器,请使用以下内容

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

我希望这能对您有所帮助


谢谢@ReinierMelian,但似乎没有起作用。 - daydr3am3r
@daydr3am3r 首先,你正在使用导航控制器吗? - Reinier Melian
不行。这就是为什么它不起作用。抱歉,我之前应该提到的。 - daydr3am3r
@daydr3am3r 我在我的两个应用程序中已经实现了这个功能,但是它们都有导航控制器。所以我会尝试在没有导航控制器的情况下进行测试,并且如果有结果的话,我会发布出来的 ;) - Reinier Melian
谢谢。我会尝试看看能否想出什么并回复。 - daydr3am3r
@daydr3am3r,请检查我的答案是否已被编辑,对我来说使用这段代码完美无缺。 - Reinier Melian

1
作为Reinier Melian帖子的更新,这是Swift 3中的UINavigationController扩展:
import UIKit

extension UINavigationController {
    override open var shouldAutorotate: Bool {
        return (self.viewControllers.last?.shouldAutorotate)!
    }

    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return (self.viewControllers.last?.supportedInterfaceOrientations)!
    }

    override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation)!
    }
}

很遗憾,如果您调用UIImagePickerController,代码会崩溃,因为它包含在最后一个视图控制器为空的UINavigationController中。

谢谢。好的,知道了。 - daydr3am3r

0

可能我有点疯狂,但为什么不使用这个呢?

enter image description here


1
问题在于@daydr3am3r只需要一个视图控制器横向显示,而你的答案使整个应用程序都横向显示。 - Reinier Melian
哦,没看到那个!现在我明白了...谢谢! - Ulli H
别担心,这种事情经常发生。 - Reinier Melian
@UlliH - 感谢您的建议。然而,该应用最初是为纵向模式设计的,现在我需要插入一个横向屏幕。 - daydr3am3r
@daydr3am3r 好的,我有点疯狂;-) - Ulli H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接