如何在Swift中以编程方式旋转方向? [英] How to rotate orientation programmatically in Swift?
本文介绍了如何在Swift中以编程方式旋转方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
viewWillAppear
但不起作用。如何在viewWillAppear上旋转我的屏幕?
but it does not work. How can I rotate my screen on viewWillAppear?
更新
我使用的是下一个锁定方向的代码:
I use the next code for locking an orientation:
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控制器中设置:
and in my FIRST controller in viewWillAppear I set:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}
所以,当我回来的时候从SECOND控制器到FIRST:
so, when I back from SECOND controller to the FIRST:
我将它旋转到左边 - 不旋转,向后旋转 - 没有,向左旋转,再旋转 - 它旋转。
I rotate it to left - does not rotate, rotate back to right - nothing, rotate to left, again - it rotates.
我该如何解决?
推荐答案
到在swift 3中以编程方式更改方向,使用以下方式:
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
您可以使用方向,
1。 portrait
2. portraitUpsideDown
3. landscapeLeft
4. landscapeRight
这篇关于如何在Swift中以编程方式旋转方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文