【问题标题】:Unable to force UIViewController orientation无法强制 UIViewController 方向
【发布时间】:2016-07-11 14:01:17
【问题描述】:

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

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

How to lock orientation of one view controller to portrait mode only in Swift

supportedInterfaceOrientationsForWindow in Swift 2.0

有什么想法吗?似乎我错过了什么,但我不知道是什么。

【问题讨论】:

    标签: ios swift uiviewcontroller screen-rotation


    【解决方案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;
        }
    

    希望对你有帮助

    【讨论】:

    • 感谢@ReinierMelian,但它似乎不起作用。
    • @daydr3am3r 首先,您使用的是导航控制器吗?
    • 不。这就是为什么不起作用。对不起。我应该之前提到过。
    • @daydr3am3r 我已经在我的 2 个应用程序中实现了这个,但是上面有导航控制器,所以我会在没有导航控制器的情况下进行检查,如果有的话,我会发布我的结果 ;)
    • 谢谢。我会尝试看看我是否也能想出一些东西并回帖。
    【解决方案2】:

    作为对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,此代码会崩溃,因为它包含在最后一个视图控制器为 nil 的 UINavigationController 中。

    【讨论】:

    • 谢谢。很高兴知道。
    【解决方案3】:

    可能我疯了 ;-),但你为什么不用这个呢?

    【讨论】:

    • 问题是@daydr3am3r 只需要 1 个 viewController 上的横向,你的回答使所有应用程序只在横向上
    • 哦,没看到! :-( 现在我明白了...谢谢!
    • 别担心它会发生
    • @UlliH - 感谢您的建议。但是,该应用最初是为仅纵向模式设计的,现在我需要插入横向屏幕。
    • @daydr3am3r 好吧,我疯了 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2016-02-21
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多