Swift 为什么我的按钮在被选中时不改变颜色?

Swift 为什么我的按钮在被选中时不改变颜色?,swift,uiviewcontroller,uibutton,storyboard,Swift,Uiviewcontroller,Uibutton,Storyboard,我有几个按钮在一个单一的视图控制器,我想改变颜色时,他们被选中。我目前的代码如下 @IBAction func academicButtonPressed(_ sender: Any) { if academicButton.backgroundColor == UIColor.systemBackground { academicButton.backgroundColor = UIColor(red: 20, green: 215, blue: 201, alpha:

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

@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)
    }
}

但它不起作用。我做错了什么?(这只是许多按钮中的一个;当选择时,它们将执行相同的操作)。

我认为您的颜色比较错误

试试这个

academicButton.backgroundColor.isEqual(UIColor.systemBackground)