【发布时间】: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