Joan Martín
Joan Martín

Reputation: 129

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

I have a black icon that and I use it to image button. Programatically I tint this button gray. When the screen is displayed, the button is gray, but once I tap button, it turns black again.

This is how I tint the button:

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

Thanks!

Upvotes: 0

Views: 77

Answers (2)

Ved Sharma
Ved Sharma

Reputation: 769

Please try this and it will resolve your issue.

Image Mode

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

Background Image mode

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

Upvotes: 2

Chaolong Liao
Chaolong Liao

Reputation: 86

Try this:

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

Upvotes: 0

Related Questions