我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
on
viewWillAppear
但它不起作用。如何在viewWillAppear上旋转屏幕?
更新
我使用下一个代码来锁定方向:
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)
}
}
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)
}
}
并在viewWillAppear的FIRST控制器中设置:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
在我的SECOND控制器中:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}
所以,当我从SECOND控制器回到FIRST:
我向左旋转 - 不旋转,向后旋转 - 没有,向左旋转,再旋转 - 旋转。
我该如何解决?
答案 0 :(得分:18)
要以swift 3 编程方式更改方向,请使用以下方法:
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
您可以使用方向
<强> 1。肖像 2. portraitUpsideDown 3. landscapeLeft 4. landscapeRight
答案 1 :(得分:1)
您可以覆盖UIViewController.supportedInterfaceOrientations()
,而不是在viewWillAppear
对于Xcode 7
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
对于Xcode 6
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
确保在项目设置中启用了所需的方向。
答案 2 :(得分:1)
快捷键4:
第1步:
在Appdelegate中-声明deviceOrientation
变量,默认情况下为portrait
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
}
}
第2步:
在您的特定ViewController中编写此代码以更改方向-
override func viewWillAppear(_ animated: Bool) {
appDelegate.deviceOrientation = .landscapeLeft
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
答案 3 :(得分:1)
可以修改它以实现方向的任意组合。
internal var viewControllerOrientation = 0
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if viewControllerOrientation == 0 {
return .portrait
} else if viewControllerOrientation == 1 {
return .landscapeLeft
} else if viewControllerOrientation == 2 {
return .landscapeRight
}
}
视图控制器被锁定为landscapeLeft方向
override func viewDidAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 1
}
@IBAction func newVCBtnWasPressed(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 0
// go to different view controller method, such as:
dismiss(animated: true, completion: nil)
}
视图控制器被锁定在landscapeRight方向
override func viewDidAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 2
}
@IBAction func newVCBtnWasPressed(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 0
// go to different view controller method, such as:
dismiss(animated: true, completion: nil)
}
所有其他视图控制器均锁定为纵向。同样,可以修改它以实现所需的锁定方向的任意组合。
答案 4 :(得分:0)
迅速5
import UIKit
extension UIViewController {
class func setUIInterfaceOrientation(_ value: UIInterfaceOrientation) {
UIDevice.current.setValue(value.rawValue, forKey: "orientation")
}
}