Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView doesn't always tint template image

In the case below, there are two UIImageViews with the same settings and the same template image... But one tints the image, and one does not

I duplicated working UIImageView and placed it instead of the other and it worked. This happened to me multiple times and this solution always worked, but I still wonder what could I have done wrong? Can it be an Xcode bug? Did something similar happen to you? I have Xcode 8.1.

Xcode screenshot

Xcode screenshot

like image 245
Zuzana Paulis Avatar asked Dec 13 '16 12:12

Zuzana Paulis


2 Answers

Easy fix solution:

enter image description here

Just add a new runtime attribute which will set the tintColor of the UIImageView to the specified color and ensure the image is tinted.

You will still need to set your image to be rendered as a template image in your Images.xcassets file.

This way you dont need any additional outlets, extensions or lines of code.

Also take note: It will not apply the tintColor in the user defined attribute if the tintColor on the view is the same color, they must be different.

like image 87
David Rees Avatar answered Oct 13 '22 15:10

David Rees


Best solution I found that doesn't require a subclass or another IBInspectable attribute:

import UIKit  extension UIImageView {     override open func awakeFromNib() {         super.awakeFromNib()         tintColorDidChange()     } } 
like image 32
allaire Avatar answered Oct 13 '22 16:10

allaire