2

I want to use "withRenderingMode(.alwaysTemplate)" to set an image on a button, but don't want to change any tint colour. Just want to set the default image.

let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red

Here I didn't want to use red tint colour. But if though I didn't use any tint colour, I didn't get actual image. I just want to set actual image.

spectre_d2
  • 193
  • 15

1 Answers1

2

If the image is already colored the way you like, then you could leave off the

withRenderingMode(.alwaysTemplate)

From documentation, Apple states that UIImage.RenderingMode.alwaysTemplate will:

Always draw the image as a template image, ignoring its color information.

Also, to ensure you get your original color you could use:

withRenderingMode(.alwaysOriginal)

Back to the documentation:

Always draw the original image, without treating it as a template.

Shawn
  • 78
  • 8