如何为按钮着色然后当您点击它时它不会改变颜色?

How to tint a button and then when you tap in it it doesn't change color?

我有一个黑色图标,我用它来显示按钮。我以编程方式将此按钮染成灰色。当屏幕显示时,按钮是灰色的,但是当我点击按钮时,它又变黑了。

这是我为按钮着色的方式:

addQuantityButton.imageView?.image = addQuantityButton.imageView?.image?.withRenderingMode(.alwaysTemplate)
addQuantityButton.imageView?.tintColor = .gray

谢谢!

试试这个:

setImage(image.withRenderingMode(.alwaysTemplate), for: .highlighted)

请试试这个,它会解决您的问题。

图像模式

   addQuantityButton.setImage(addQuantityButton.currentImage!.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
    addQuantityButton.tintColor = .gray

背景图像模式

addQuantityButton.setImage(addQuantityButton.currentBackgroundImage!.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
    addQuantityButton.tintColor =.gray