【发布时间】:2022-01-21 16:03:45
【问题描述】:
我正在尝试学习 iOS 编程。首先,我想在单击按钮时更改视图的背景颜色。我的整个代码如下所示,但是当我单击按钮时,不会发生任何变化。我正在使用 Xcode 13.1。我也在使用故事板。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSLog("The app has launched")
}
@IBAction func changeColorClicked(_ sender: UIButton) {
self.myBtn.backgroundColor=UIColor.green
NSLog("I want to change color to green")
}
}
单击时我看到了 NSLog 消息,只是颜色没有变化。
【问题讨论】:
-
我可以更改标题文本的颜色,但不能更改按钮的背景:
myBtn.setTitleColor(UIColor.red, for: .normal)