Using hue colors in a UIImageView

I have my own subclass of UIButton . I add a UIImageView to it and add an image. I would like to draw it on top of the image with a tint of color, but this does not work.

So far I have:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.clipsToBounds = YES; self.circleView = [[UIView alloc]init]; self.circleView.backgroundColor = [UIColor whiteColor]; self.circleView.layer.borderColor = [[Color getGraySeparatorColor]CGColor]; self.circleView.layer.borderWidth = 1; self.circleView.userInteractionEnabled = NO; self.circleView.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:self.circleView]; self.iconView = [[UIImageView alloc]init]; [self.iconView setContentMode:UIViewContentModeScaleAspectFit]; UIImage * image = [UIImage imageNamed:@"more"]; [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; self.iconView.image = image; self.iconView.translatesAutoresizingMaskIntoConstraints = NO; [self.circleView addSubview:self.iconView]; ... 

and to choose from:

 - (void) setSelected:(BOOL)selected { if (selected) { [self.iconView setTintColor:[UIColor redColor]]; [self.circleView setTintColor:[UIColor redColor]]; } else{ [self.iconView setTintColor:[UIColor blueColor]]; [self.circleView setTintColor:[UIColor blueColor]]; } } 

What have I done wrong? (The color of the image always remains the same.)

+94
ios xcode uibutton uiimageview tintcolor
Mar 04 '14 at 11:27
source share
10 answers

Instead of this code:

 [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

you should have:

 image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

Use it in Swift 4.1

 image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate) 
+195
Mar 04 '14 at 12:22
source share

You can also just set this on your asset. Make sure your image contains all white pixels + transparent. enter image description here

+65
Nov 08 '16 at 10:34
source share

(Unable to edit @Zhaolong Zhong's post)

In quick 3.0 you can do:

 let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate) yourImageView.image = image yourImageView.tintColor = UIColor.blue 
+30
05 Oct '16 at 13:46 on
source share

In swift 2.0+ you can:

 let image = UIImage(named: "your_image_name")!.imageWithRenderingMode(.AlwaysTemplate) yourImageView.image = image yourImageView.tintColor = UIColor.blueColor() 
+19
Jun 24 '16 at 21:32
source share

Goal c

 self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [self.imgView setTintColor:[UIColor darkGrayColor]]; 
+9
Mar 09 '17 at 7:33
source share

One more step. This is a subdirectory of UIImageView. (Inaccurate solution for the original question.) Use in Interface Builder by setting the class name in TintedImageView. Real-time updates within the designer as color shades change.

(Swift 3.1, Xcode 8.3)

 import UIKit @IBDesignable class TintedImageView: UIImageView { override func prepareForInterfaceBuilder() { self.configure() } override func awakeFromNib() { super.awakeFromNib() self.configure() } @IBInspectable override var tintColor: UIColor! { didSet { self.configure() } } private func configure() { self.image = self.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate) } } 
+5
Apr 02 '17 at 19:53 on
source share

Make Image View

  let imageView = UIImageView(frame: frame!) imageView.contentMode = .ScaleAspectFit imageView.tintColor = tintColor 

Take a picture

  let mainBundle = NSBundle.mainBundle() var image = UIImage(named: filename!, inBundle: mainBundle, compatibleWithTraitCollection: nil) image = image?.imageWithRenderingMode(.AlwaysTemplate) 

Put them together

  imageView?.image = image 

Display it

 view.addSubview(imageView) 
+3
Nov 10 '17 at 3:38 on
source share

everything is said correctly. my contribution If you can not / do not want to apply to each UiImageView, OR for efficiency, you need to do it ONCE (or an example for table cells)

 func tint(with color: UIColor) -> UIImage { var image = withRenderingMode(.alwaysTemplate) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.set() image.draw(in: CGRect(origin: .zero, size: size)) image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image } 

And set this UIImage for all user interface elements.

+1
Mar 07 '18 at 10:08
source share

@Odemolliens answer should just work.

But if you still have problems, make sure that the color of the hue that you apply to the UIImageView is different from the color defined in Interface Builder.

+1
Apr 20 '18 at
source share

I just found out that my hue did not change in ViewDidLoad (or even installed it in Interface Builder). A call in sight WillAppear solved my problem!

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) myImageView.tintColor = UIColor.white } 
0
Apr 20 '19 at 21:33
source share



All Articles