I have an UIImageView called "theImageView", with UIImage in a single color (transparent background) just like the left black heart below. How can I change the tint color of this image programmatically in iOS 7 or above, as per the tint method used in the iOS 7+ Navigation Bar icons?
Can this method also work in WatchKit for an Apple Watch app?
If anyone care a solution without
UIImageView
:iOS
For an iOS app, in Swift 3 or 4:
Swift 2:
Meanwhile, the modern Objective-C solution is:
Watchkit
In WatchKit for Apple Watch apps, you can set the tint color for a template image.
To then set the tint color in Swift 3 or 4:
Swift 2:
To then set the tint color in Objective-C:
If you use a template image and do not apply a tint colour, the Global Tint for your WatchKit app will be applied. If you have not set a Global Tint,
theImage
will be tinted light blue by default when used as a template image.This is my UIImage extension and you can directly use changeTintColor function for an image.
Example usage like this
And you can use change
changeColor
function to change the image colorFor swift 3 purposes
theImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate) theImageView.tintColor = UIColor.red
Here's a category that should do the trick
so you would do: