无法强制UIViewController方向

我有一个景观ViewController的肖像应用程序。

我一直在挖掘很多关于如何强制方向横向时,应用程序被locking到肖像,我尝试了很多这里提出的解决scheme,不仅如此,但没有任何运气。

到目前为止,我设法自动旋转风景中需要的VC,但由于方向未locking,所有其他VC也将旋转。

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

经过更多的挖掘后,我find了一个示例项目,但当它在该项目中工作后,这似乎并没有为我工作。

这是在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方法不validation初始条件。

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

如何在Swift中将一个视图控制器的方向locking到肖像模式

在Swift 2.0中的supportedInterfaceOrientationsForWindow

有任何想法吗? 看来我错过了一些东西,但我无法弄清楚什么。

试试这只强制到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; } 

我希望这可以帮助你

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

在这里输入图像说明

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