【发布时间】:2021-05-29 05:16:18
【问题描述】:
例如,我有两个按钮(按钮 1 和按钮 2)。
- 按钮 1 已被选中,其背景颜色为红色,文本颜色为白色。
- 按钮 2 背景颜色为灰色,文本颜色为红色。
如果我点击按钮 2,那么背景颜色将为红色,文本颜色为白色。
如果我点击按钮 1,它将变为灰色背景色和红色文本颜色。
如果我单击按钮 1,则背景颜色将为红色,文本颜色为白色,按钮 2 将变为灰色背景颜色和红色文本颜色。
那么如何以编程方式做到这一点?
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button1: UIButton!
@IBAction func btnbutton2(_ sender: Any) {
if (buttonTest2.isSelected == false){
button2.backgroundColor = UIColor.gray
button2.setTitleColor(UIColor.red, for: .normal)}
if (button2.isSelected == true){
button2.backgroundColor = UIColor.systemRed
button2.setTitleColor(UIColor.white, for: .normal)}
}
@IBAction func btnbutton1(_ sender: Any) {
if (button1.isSelected == true){
button1.backgroundColor = UIColor.systemRed
button1.setTitleColor(UIColor.white, for: .normal)}
if (button1.isSelected == false){
button1.backgroundColor = UIColor.gray
button1.setTitleColor(UIColor.red, for: .normal)}
}
override func viewDidLoad() {
super.viewDidLoad()
button1.backgroundColor = UIColor.systemRed
button1.setTitleColor(UIColor.white, for: .normal)
button2.backgroundColor = UIColor.gray
button2.setTitleColor(UIColor.red, for: .normal)}
【问题讨论】:
-
请出示您的代码,以展示您到目前为止所做的尝试。
-
请在此处发布之前尝试搜索和阅读文档...
button.backgroundColor = .red和button.setTitleColor(.white, for: []) -
@creeperspeak 请检查更新后的问题以及我已完成的代码