12

I have a UITableView which is comprised of static cells. In IB I've set each UITableViewCell's style to "Basic" and set an image (see screenshot). The buttons in my nav bar honor the tintColor property but the images in the tableview do not. Thus far I've done everything in IB - do I have to use code if I want the images to honor the tintColor property too?

Thanks

enter image description here

RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
  • 1
    http://stackoverflow.com/questions/18855080/dimming-a-tintcolor-on-a-uitableviewcell – Retro Sep 15 '14 at 13:43
  • So this can't be done in IB? That seems really odd since, IMHO, this is normally what I'd want to happen by default. And since these are static cells I really don't want to have to do everything in code if I don't have to. – RobertJoseph Sep 15 '14 at 13:46
  • 1
    cellImageView dont have tint color property. you have to use custom cell and little code. http://stackoverflow.com/questions/5145953/how-to-tint-an-image-show-a-colour – Retro Sep 15 '14 at 13:59
  • If you don't want do anything in code why not change the image original color as you want? – dopcn Sep 15 '14 at 14:26
  • @dopcn I'd like to be able to change the color easily later. So creating my images with a specific color would be troublesome. – RobertJoseph Sep 15 '14 at 14:44

4 Answers4

10

Every UIImageView has the property tintColor on iOS7+

Try to set cell's imageView.image.renderingMode to UIImageRenderingModeAlwaysTemplate in storyboard User Defined Runtime Attributes

dopcn
  • 4,218
  • 3
  • 23
  • 32
  • 2
    i am using vector pdfs which are rendered as template. they show up fine with the set tint color everywhere except for on of my custom cells where the images are inside an image view. how do i deal with this? – AceN Mar 18 '15 at 05:40
  • I have the same problem with image views contained in a table cell. I usually just use a UIButton instead and just set its interactionEnabled to false. There's virtually no difference except that a button doesn't always hug the exact size of the image specially the very small ones. – riadhluke Apr 15 '16 at 06:55
9

What a headache this problem is after so many versions! The only thing that is a sure fire fix, assuming everything is "right" (template image, etc) is subclassing the UIImageView and overriding didMoveToSuperview with this...

- (void)didMoveToSuperview
{
    [self setHighlighted:YES];
    [self setHighlighted:NO];
    [super didMoveToSuperview];
}

It's a hack but it works.

Sean
  • 189
  • 3
  • 7
  • This hack works well in such case that tint color is applied only after tap the cell. Thanks! – nopopon Apr 13 '17 at 11:41
  • Same. In the scenario where the UIImageView was in a UICollectionViewCell and was not being highlighted until after the cell was tapped, adding this to my UIImageView subclass did create the correct effect. *sigh...* – Matt Mc Feb 28 '18 at 01:58
2

Here is an example:

UIImage *image = [[UIImage imageNamed:@"ic_email_white"]
                  imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[[cell imageView] setImage:image];
[[cell imageView] setTintColor:[UIColor redColor]];
meda
  • 45,103
  • 14
  • 92
  • 122
1

The below setup worked for me well.

In the storyboard, set cell's contentView tintColor as your desired color and UIImagview's tintColor as default.

john raja
  • 509
  • 4
  • 8