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?
profileImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate)
profileImageView.tintColor = UIColor.green
OR
First select Particular image in image asset and then select reddened as Template instead of Default and after that write line. profileImageView.tintColor = UIColor.green
For tinting the image of a UIButton
I had to do this in Swift using an
extension
.I thought I'd share how I did it:
Usage:
theImageView.image = theImageView.image.imageWithColor(UIColor.redColor())
Swift 4
Usage:
theImageView.image = theImageView.image?.imageWithColor(color1: UIColor.red)
Now i use this method based in Duncan Babbage response:
Take benefit of Extension in Swift :-
Swift 3
version of extension answer from fuzz