无法强制UIViewController方向

时间:2016-07-11 14:01:18

标签: ios swift uiviewcontroller screen-rotation

我有一个带有一个横向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

有什么想法吗?我似乎错过了一些东西,但我无法弄清楚是什么。

3 个答案:

答案 0 :(得分: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())!;
    }

}

EDITED

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

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

我希望这有助于你

答案 1 :(得分:0)

可能我很疯狂;-),但为什么不使用它?

enter image description here

答案 2 :(得分:0)

作为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中。