在Swift iOS中设置设备方向
我正在为iPhone快速开发一个应用程序。 在我的应用程序中有一个模式视图,我只想在纵向视图。
我的问题是,我如何以编程方式强制手机不允许旋转? 换句话说,我正在寻找不允许在横向模式下显示模态视图的代码(打开纵向旋转locking)。
这只是1模态视图,所以我不能closures整个应用程序的旋转,否则我只是完全禁用旋转。
我在我的研究中发现了代码但是它是在客观的C中,以防万一。 谢谢!
您可以将这些方法粘贴到需要纵向显示的每个视图的ViewController中:
override func shouldAutorotate() -> Bool { return false } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait }
嗨LandscapeLeft和LandscapeRight (更新Swift 2.0)
你有这个信息
和UIController
override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight] }
对于PortraitUpsideDown和Portrait使用它
override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft || UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return false } else { return true } } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown] }
来自法国的消息,圣诞快乐!
编辑:
其他解决scheme
extension UINavigationController { public override func shouldAutorotate() -> Bool { if visibleViewController is MyViewController { return true // rotation } else { return false // no rotation } } public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return (visibleViewController?.supportedInterfaceOrientations())! } }
Swift 3
如果在UINavigationController或UITabBarController中embedded视图控制器,则方向旋转将更加复杂,导航或选项卡栏控制器将优先考虑并决定自动旋转和支持的方向。
在UINavigationController和UITabBarController上使用以下扩展,以便embedded在其中一个控制器中的视图控制器可以做出决定:
UINavigationController扩展
extension UINavigationController { override open var shouldAutorotate: Bool { get { if let visibleVC = visibleViewController { return visibleVC.shouldAutorotate } return super.shouldAutorotate } } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { if let visibleVC = visibleViewController { return visibleVC.preferredInterfaceOrientationForPresentation } return super.preferredInterfaceOrientationForPresentation } } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { if let visibleVC = visibleViewController { return visibleVC.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } }}
UITabBarController扩展
extension UITabBarController { override open var shouldAutorotate: Bool { get { if let selectedVC = selectedViewController{ return selectedVC.shouldAutorotate } return super.shouldAutorotate } } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { if let selectedVC = selectedViewController{ return selectedVC.preferredInterfaceOrientationForPresentation } return super.preferredInterfaceOrientationForPresentation } } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { if let selectedVC = selectedViewController{ return selectedVC.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } }}
现在,您可以覆盖要locking的视图控制器中的supportedInterfaceOrientations,shouldAutoRotate和preferredInterfaceOrientationForPresentation,否则,您可以忽略其他视图控制器中您要inheritance应用程序的plist中指定的默认方向行为的覆盖。
locking具体的方向
class YourViewController: UIViewController { open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { return .portrait } }}
禁用旋转
class YourViewController: UIViewController { open override var shouldAutorotate: Bool { get { return false } }}
更改首选的界面方向进行演示
class YourViewController: UIViewController { open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { return .portrait } }}
如果您的视图控制器属于导航控制器,则上面的代码可能无法正常工作。 如果是的话,即使它本身有不同的方向规则,也必须遵守导航控制器的规则。 更好的方法是让视图控制器自己决定,导航控制器将使用最顶层视图控制器的决定。
我们可以同时支持locking到当前的方向和自动旋转locking在UINavigationController这个通用扩展的特定方向: – :
extension UINavigationController { public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return (visibleViewController?.supportedInterfaceOrientations())! } }
现在在你的视图控制器里面
class ViewController: UIViewController { // MARK: Autoroate configuration override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return true } else { return false } } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) } }
希望它有帮助。 谢谢
如果有人想要答案,我想我刚刚得到了答案。 尝试这个:
- 去你的.plist文件,并检查所有的方向。
- 在你想强制方向的视图控制器中,添加下面的代码:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait.toRaw().hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.toRaw().hashValue }
希望它有帮助!
编辑:
要强制旋转,请使用以下代码:
let value = UIInterfaceOrientation.LandscapeRight.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation")
它适用于iOS 7和8!
这将禁用视图的自动旋转:
override func shouldAutorotate() -> Bool { return false; }
更新
override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft || UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return false; } else { return true; } }
如果应用程序处于横向模式,并显示必须以纵向模式显示的视图,则这将允许应用程序将其方向更改为纵向(当然,当设备将旋转到此方向时)。
Go to your pList and add or remove the following as per your requirement: "Supported Interface Orientations" - Array "Portrait (bottom home button)" - String "Portrait (top home button)" - String "Supported Interface Orientations (iPad)" - Array "Portrait (bottom home button)" - String "Portrait (top home button)" - String "Landscape (left home button)" - String "Landscape (right home button)" - String
注意:这个方法允许整个应用程序的旋转。
要么
在项目中创buildUIViewController的ParentViewController(inheritance方法)。
// UIappViewController.swift import UIKit class UIappViewController: UIViewController { super.viewDidLoad() } //Making methods to lock Device orientation. override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } override func shouldAutorotate() -> Bool { return false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
将每个视图控制器的父控制器关联为UIappViewController。
// LoginViewController.swift import UIKit import Foundation class LoginViewController: UIappViewController{ override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
对于Swift 3,iOS 10
override open var shouldAutorotate: Bool { return false } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait }
但是,设置shouldAutorotate
目前不能在iOS 9上运行。
在info.plist文件中,在“支持的接口方向”中更改所需的方向。
以迅捷的方式支持文件 – > info.plist->支持界面方向。
我一直在苦苦挣扎,只得到正确的左/右支持的景观。 我发现了一件非常烦人的事情。 虽然“常规”选项卡允许您取消select“纵向”以进行设备方向,但您必须编辑自定义纵向以禁用纵向和纵向上下界面方向 – 这是“立体图”中的最后一个键:“支持的界面方向”。
另一件事是,你似乎必须使用枚举的“面具”版本(例如,UIInterfaceOrientationMask.LandscapeLeft),而不仅仅是方向之一。 为我工作的代码(在我的主视图控制器):
override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue) }
让plist更改和代码的组合是我能够正确工作的唯一方法。
更像Swift的版本:
override func shouldAutorotate() -> Bool { switch UIDevice.currentDevice().orientation { case .Portrait, .PortraitUpsideDown, .Unknown: return true default: return false } } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) }
来自Vivek Parihar的UINavigationController
extension UINavigationController { public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } }
// Swift 2
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown] return orientation }
两个build议@Vivek Parihar的解决scheme:
-
如果我们展示任何viewController,我们应该在navigationController扩展中检查“visibleViewController”的nil
extension UINavigationController { public override func shouldAutorotate() -> Bool { var shouldAutorotate = false if visibleViewController != nil { shouldAutorotate = visibleViewController.shouldAutorotate() } return shouldAutorotate } public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations() } }
-
如果我们正在使用任何操作表呈现,并且用户将向上旋转,则您的操作表将从屏幕的上边缘打开:P,要解决这个问题,我们应该只使用肖像
override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return true } else { return false }
}
override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) }
Swift 2.2
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if self.window?.rootViewController?.presentedViewController is SignatureLandscapeViewController { let secondController = self.window!.rootViewController!.presentedViewController as! SignatureLandscapeViewController if secondController.isPresented { return UIInterfaceOrientationMask.LandscapeLeft; } else { return UIInterfaceOrientationMask.Portrait; } } else { return UIInterfaceOrientationMask.Portrait; } }
我的微薄贡献(Xcode 8,Swift 3):
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) { if (rootViewController.responds(to: Selector(("canRotate")))) { // Unlock landscape view orientations for this view controller return .allButUpsideDown; } } return .portrait; } private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? { if (rootViewController == nil) { return nil } if (rootViewController.isKind(of: (UITabBarController).self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController) } else if (rootViewController.isKind(of:(UINavigationController).self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController) } else if (rootViewController.presentedViewController != nil) { return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController) } return rootViewController }
…在AppDelegate上。 所有甘地梅纳的积分: http : //www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift /
从ios 10.0开始,我们需要set { self.orientations = newValue }
来设置方向,确保在您的项目中启用了横向属性。
private var orientations = UIInterfaceOrientationMask.landscapeLeft override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.orientations } set { self.orientations = newValue } }