UIImageView doesn't always tint template image
Solution 1
Easy fix solution:
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.
Solution 2
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()
}
}
Solution 3
The problem stems from the value for tint having no null state (within the Storyboard/Interface Builder UI). In order for Xcode to decide whether a tint should be applied to a UIImageView
there appears to be an additional test. In Xcode this is apparently done by evaluating the constraints applied to UIImageView
. As far as I know this test is undocumented.
If you only have a "Spacing to nearest neighbor" constraint on two (or one) sides of your UIImageView
then the tint is not applied regardless of the value set for UIImage.renderingMode
.
If you have a "Spacing to nearest neighbor" constraint on three (or all) sides of your UIImageView
then the tint is applied if the UIImage.renderingMode
is set to .alwaysTemplate
.
In a purely Storyboard/Interface Builder approach you set the UIImage.renderingMode
of an image by adding it to an Asset Catalogue and then changing the "Render As" property of the Image Set to "Template Image".
Solution 4
Try setting tintColor
programmatically; it fixed the issue for me.
Solution 5
I think there is a bug in UIImageView. If you set a tint color in the storyboard, it doesn't apply that tint even when the asset is set up to render as template.
The workaround is to apply the tint color to the UIImageView's parent view in the storyboard.
Or, set the tintColor to nil and then back again in code by binding the UIImageView to an outlet.
Comments
-
Zuzana Paulis over 2 years
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.