我有一个UIViewController,我想在不同的场景中禁用或启用屏幕旋转
例:
if flag {
rotateDevice = false
}
else {
rotateDevice = true
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
Bao*_*iep 42
我有答案.在AppDelegate,如果您旋转设备,请按viewcontroller等.此功能始终调用
更新swift 3/4
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}
Run Code Online (Sandbox Code Playgroud)
self.restrictRotation 是一个自定义参数.
如何使用:
在Appdelegate:
var restrictRotation:UIInterfaceOrientationMask = .portrait
Run Code Online (Sandbox Code Playgroud)
在ViewController中:
调用方法ViewDidLoad或viewWillAppear时.我们将改变如下:
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all
Run Code Online (Sandbox Code Playgroud)
然后将调用AppDelegate上的此方法.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
Run Code Online (Sandbox Code Playgroud)
Jul*_*ere 24
你只需要实现shouldAutorotate并supportedInterfaceOrientations进入你的UIViewController.看一下这个文档.例如:
override func shouldAutorotate() -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
您还可以指定可用的方向.这是一个只有肖像方向的例子:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想支持关于旗帜的不同方向,你只需要做这样的事情:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if myFlag {
return .Landscape
} else {
return .All
}
}
Run Code Online (Sandbox Code Playgroud)
(如果myFlag是,它将允许Landscape方向.否则,它将允许所有方向).
Bar*_*Lee 14
迅速4
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
Run Code Online (Sandbox Code Playgroud)
cat*_*ore 10
一个简单的解决方案,可满足您的最常见要求(iPhone:纵向,iPad:全部)AppDelegate:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}
Run Code Online (Sandbox Code Playgroud)
在Swift 5中,如前所述,如果要允许(避免其他)特定的UIViewController沿某个方向旋转,则必须重写UIViewController类中的“ supportedInterfaceOrientations”,如下所示:
class MyViewController:UIViewController{
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return .portrait
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里有可能的选项:
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 }
}
Run Code Online (Sandbox Code Playgroud)
扩展的
如果要区分iPad或iPhone,可以使用UIUserInterfaceIdiom:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all //OBS -> You can also return an array
}
}
Run Code Online (Sandbox Code Playgroud)
哪里:
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35211 次 |
| 最近记录: |