gpt4 book ai didi

ios - 如何在 Swift 中以编程方式旋转方向?

转载 作者:IT王子 更新时间:2023-10-29 05:46:53 24 4
gpt4 key购买 nike

我尝试了下一个:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")

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 的第一个 Controller 中,我设置了:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}

在我的第二个 Controller 中:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}

所以,当我从第二个 Controller 返回到第一个 Controller 时:

我将它向左旋转 - 不旋转,向右旋转 - 没有,向左旋转,再次 - 它旋转。

我该如何解决?

最佳答案

要在 swift 3 中以编程方式更改方向,请使用以下方式:

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

您可以使用方向,

<强>1。肖像2.人像颠倒3.景观左4.横向右

关于ios - 如何在 Swift 中以编程方式旋转方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com