UIImageView tintColor colors whole image

581 views Asked by At

I've tried changing the tintColor of a UIImageView inside a UICollectionViewCell after a tap by setting render mode of the icons to Template Image and then setting the color by calling

iconView.tintColor = .blue

Problem: This fills the whole image with the chosen color, Instead of just changing the non-transparent parts.

This method has been working for me in the past, and I'm unable to figure out the problem. I assumed that the icons might have a white background instead of a transparent one, but that's not the case. Maybe it has something to do with the UIImageView being inside a UICollectionViewCell?

Help would be highly appreciated!

2

There are 2 answers

2
Gurinder Batth On

try this

imageViewHome.image = UIImage.init(named: "yourImageName")?.withRenderingMode(.alwaysTemplate)

imageView.tintColor = .blue
0
Aman Gupta On

The following code will color only the non transparent area.

yourImage.image = yourImage.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
yourImage.tintColor = UIColor.redColor()

Happy coding.