Using Tint color on UIImageView
I have my own subclass of UIButton
. I add UIImageView
on it and add an image. I would like to paint it over the image with a tint color but it doesn't 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 on selection :
- (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 did I do wrong? (The color of the image always stays the same as it was originally.)
Solution 1:
Instead of this code:
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
you should have:
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
Use this in Swift 4.1
image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)
Solution 2:
You can also just set this on your asset. Make sure your image contains all white pixels + transparent.