Ork*_*ade 8 orientation ios swift
我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
上
viewWillAppear
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何在viewWillAppear上旋转屏幕?
UPDATE
我使用下一个代码来锁定方向:
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的第一个控制器viewWillAppear我设置:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)
在我的SECOND控制器中:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}
Run Code Online (Sandbox Code Playgroud)
所以,当我从SECOND控制器回到FIRST:
我向左旋转 - 不旋转,向后旋转 - 没有,向左旋转,再旋转 - 旋转.
我该如何解决?
Ram*_*van 20
要在swift 3中以编程方式更改方向,请使用以下方法:
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
你可以使用方向,
1. portrait 2. portraitUpsideDown 3. landscapeLeft 4. landscapeRight
斯威夫特 4:
第1步:
在 Appdelegate 中 - 声明默认情况下的deviceOrientationvarportrait
import UIKit
let appDelegate = UIApplication.shared.delegate as! AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var deviceOrientation = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return deviceOrientation
}
}
Run Code Online (Sandbox Code Playgroud)
第2步:
在你的某个 ViewController 中写这个来改变方向 -
override func viewWillAppear(_ animated: Bool) {
appDelegate.deviceOrientation = .landscapeLeft
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
Run Code Online (Sandbox Code Playgroud)
您可以覆盖UIViewController.supportedInterfaceOrientations()而不是触发旋转viewWillAppear
对于 Xcode 7
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
Run Code Online (Sandbox Code Playgroud)
对于 Xcode 6
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
Run Code Online (Sandbox Code Playgroud)
并确保在项目设置中启用所需的方向。
| 归档时间: |
|
| 查看次数: |
16180 次 |
| 最近记录: |