IOS。如何在每个 UIViewController 上启用和禁用旋转?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38969419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS. How to enable and disable rotation on each UIViewController?
提问by Bao Tuan Diep
I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios
我有一个 UIViewController,我想在不同场景下禁用或启用屏幕旋转
Example:
例子:
if flag {
rotateDevice = false
}
else {
rotateDevice = true
}
How can I do that?
我怎样才能做到这一点?
回答by Bao Tuan Diep
I have the answer.
On AppDelegate
, if you rotate device, push viewcontroller, etc. This function always call
我有答案。On AppDelegate
,如果你旋转设备,推视图控制器等。这个函数总是调用
Update for swift 3/4
快速 3/4更新
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}
self.restrictRotation
is a custom parameter.
self.restrictRotation
是自定义参数。
How to use:
如何使用:
In Appdelegate:
在 Appdelegate 中:
var restrictRotation:UIInterfaceOrientationMask = .portrait
In ViewController:
在视图控制器中:
When method ViewDidLoad or viewWillAppear is called. We will change like this:
当调用方法 ViewDidLoad 或 viewWillAppear 时。我们会像这样改变:
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all
and then this method on AppDelegate will be called.
然后将调用 AppDelegate 上的此方法。
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
回答by Julien Quere
You simply have to implement shouldAutorotate
and supportedInterfaceOrientations
into your UIViewController
. Take a look at this documentation. For example:
您只需实现shouldAutorotate
并supportedInterfaceOrientations
进入您的UIViewController
. 看看这个文档。例如:
override func shouldAutorotate() -> Bool {
return true
}
You can also specify which orientations are available. Here is an example with only portraits orientations:
您还可以指定哪些方向可用。这是一个只有纵向方向的示例:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
Edit: If you want to support different orientation regarding a flag, you just have to do something like this:
编辑:如果你想支持关于标志的不同方向,你只需要做这样的事情:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if myFlag {
return .Landscape
} else {
return .All
}
}
(If myFlag
is true, it will allow Landscape
orientation. Otherwise, it will allow all orientations).
(如果myFlag
为真,它将允许Landscape
定向。否则,它将允许所有定向)。
回答by Baryon Lee
swift 4
迅捷 4
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
回答by Reimond Hill
In swift 5, as from previous, if you want to allow (avoid others) a particular UIViewController to rotate in a certain orientation you have to override "supportedInterfaceOrientations" inside your UIViewController class like so:
在 swift 5 中,和以前一样,如果您想允许(避免其他人)特定的 UIViewController 以特定方向旋转,您必须像这样覆盖 UIViewController 类中的“supportedInterfaceOrientations”:
class MyViewController:UIViewController{
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return .portrait
}
}
}
Here there are the possible options:
这里有可能的选项:
public struct UIInterfaceOrientationMask : OptionSet {
public init(rawValue: UInt)
public static var portrait: UIInterfaceOrientationMask { get }
public static var landscapeLeft: UIInterfaceOrientationMask { get }
public static var landscapeRight: UIInterfaceOrientationMask { get }
public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
public static var landscape: UIInterfaceOrientationMask { get }
public static var all: UIInterfaceOrientationMask { get }
public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}
Extended
扩展
If you want to be able to distinguish between iPad or iPhone you could use UIUserInterfaceIdiom:
如果您希望能够区分 iPad 或 iPhone,您可以使用 UIUserInterfaceIdiom:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all //OBS -> You can also return an array
}
}
where:
在哪里:
public enum UIUserInterfaceIdiom : Int {
case unspecified
@available(iOS 3.2, *)
case phone // iPhone and iPod touch style UI
@available(iOS 3.2, *)
case pad // iPad style UI
@available(iOS 9.0, *)
case tv // Apple TV style UI
@available(iOS 9.0, *)
case carPlay // CarPlay style UI
}
回答by catanore
An easy solution for the most common request (iPhone: portrait, iPad: all) in your AppDelegate
:
最常见请求(iPhone:纵向,iPad:全部)的简单解决方案AppDelegate
:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}