无法强制 UIViewController 方向
Unable to force UIViewController orientation
我有一个纵向应用程序和一个横向应用程序 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 方法未验证初始条件。
我也尝试了这些,但没有成功:
有什么想法吗?似乎我错过了什么,但我不知道是什么。
试试这个只强制进入 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;
}
希望对您有所帮助
可能我疯了 ;-),但你为什么不用这个?
作为 Reinier Melian 的 post 的更新,这里是 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 中。
我有一个纵向应用程序和一个横向应用程序 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 方法未验证初始条件。
我也尝试了这些,但没有成功:
有什么想法吗?似乎我错过了什么,但我不知道是什么。
试试这个只强制进入 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;
}
希望对您有所帮助
可能我疯了 ;-),但你为什么不用这个?
作为 Reinier Melian 的 post 的更新,这里是 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 中。