gpt4 book ai didi

ios - 获取当前设备方向

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:21 24 4
gpt4 key购买 nike

我知道之前有人问过这个问题并尝试实现建议的解决方案,但它对我不起作用。也许我做错了。你有什么想法吗?

这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var test: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

test.backgroundColor = UIColor.darkGray
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) // No need for semicolon

func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
test.backgroundColor = UIColor.purple
} else {
test.backgroundColor = UIColor.blue
}
}
}
}

最佳答案

你有嵌套的函数——那是行不通的。将其更改为:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) // No need for semicolon
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
test.backgroundColor = UIColor.purple
} else {
test.backgroundColor = UIColor.blue
}
}

事实上,您可以使用您显示的代码摆脱 viewWillLayoutSubviews 的覆盖。

关于ios - 获取当前设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659646/

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