【问题标题】:Why are my buttons not changing color when selected?为什么我的按钮在选择时没有改变颜色?
【发布时间】:2021-02-15 09:48:24
【问题描述】:

我在单个视图控制器上有几个按钮,我想在它们被选中时更改颜色。我目前的代码如下

@IBAction func academicButtonPressed(_ sender: Any) {
    if academicButton.backgroundColor == UIColor.systemBackground {
        academicButton.backgroundColor = UIColor(red: 20, green: 215, blue: 201, alpha: 1)
        academicButton.setTitleColor(UIColor.systemBackground, for: .selected)
    } else {
        academicButton.backgroundColor = UIColor.systemBackground
        academicButton.setTitleColor(UIColor(red: 20, green: 115, blue: 201, alpha: 1), for: .selected)
    }
}

但它不起作用。我究竟做错了什么? (这只是众多按钮中的一个;它们在被选中时都会做同样的事情)。

【问题讨论】:

    标签: swift uiviewcontroller uibutton storyboard


    【解决方案1】:

    我认为你的颜色比较错误。

    试试这个;

    academicButton.backgroundColor.isEqual(UIColor.systemBackground)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多