Marko Zadravec Marko Zadravec - 5 months ago 26
iOS Question

Using Tint color on UIImageView

I have my own subclass of

UIButton
. I add
UIImageView
on it and add image. I would like to paint it over the image with 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 I did wrong? (color of image stays always the same-as it was original)

Answer

I found the answer and it is stupid mistake, but for others who can't figure this out either.

insted :

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

you should have :

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

and this is it.

Comments