无法强制UIViewController方向 [英] Unable to force UIViewController orientation
问题描述
我有一个带有一个风景ViewController的肖像应用程序。
I have a portrait application with one landscape ViewController.
当应用程序锁定到肖像时,我一直在挖掘如何强制定位到横向的很多东西我尝试了很多这里提出的解决方案,不仅但到目前为止还没有任何运气。
I've been digging a lot on how to force the orientation to landscape when the app is locked to portrait and I tried a lot of solutions presented here and not only but without any luck so far.
到目前为止,我设法自动旋转了我需要的风景,但是从定向开始没有锁定,所有其他风投也会轮换。
So far I managed to autorotate the VC I need in landscape but since the orientation is not locked, all other VCs will also rotate.
override func viewDidAppear(animated: Bool)
{
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool
{
return true
}
经过多次挖掘在找到一个示例项目之后我最终得到了这个,但是当它在该项目中工作时,它似乎对我不起作用。
After some more digging I ended up with this after finding a sample project but while it works in that project, it doesn't seem to work for me.
这是在AppDelegate
This is in 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 :
This is in the VC I want to have in landscape:
var isPresented = true
@IBAction
func dismiss()
{
isPresented = false
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil);
}
由于某种原因,supportedInterfaceOrientationsForWindow方法不验证初始条件。
For some reason, the supportedInterfaceOrientationsForWindow method does not validate the initial condition.
我也试过这些但没有运气:
I also tried these among others but no luck:
在Swift 2.0中 supportedInterfaceOrientationsForWindow
supportedInterfaceOrientationsForWindow in Swift 2.0
任何想法?我似乎错过了一些东西,但我无法弄清楚是什么。
Any ideas? It seems I'm missing something but I can't figure it out what.
推荐答案
试试这个强制到 LandscapeRight
仅限模式
try this for force to LandscapeRight
mode only
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")
}
}
然后使用类似这样的类别
and then use a category like this
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
If you are not using navigation controller use this
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;
}
我希望这可以帮到你
这篇关于无法强制UIViewController方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!