小编典典

无法强制UIViewController方向

swift

我有一个带有横向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方法无法验证初始条件。

有任何想法吗?似乎我遗漏了一些东西,但无法弄清楚是什么。


阅读 286

收藏
2020-07-07

共1个答案

小编典典

尝试将此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;
    }

我希望这可以帮助你

2020-07-07