简体   繁体   中英

Tint color of image not changing

I have a menu bar made with collection view. Each cell contains a image (png format)

I tried to set the tint in code as shown below but the tint color of icon is not changing.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuCell *cell = [collectionView
                                 dequeueReusableCellWithReuseIdentifier:self.cell
                                 forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor blueColor];

    cell.menuCellIcon.image = [UIImage
                               imageNamed:[self.menuCellImages objectAtIndex:indexPath.item] ];
    [cell.menuCellIcon.image
     imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    cell.menuCellIcon.tintColor = [UIColor whiteColor];
...

This line... [cell.menuCellIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Returns a new image that uses rendering mode template. You are currently not doing anything with the result of this code.

You should fix your code like this...

cell.menuCellIcon.image = [[UIImage 
                           imageNamed:self.menuCellImages[indexPath.item]] 
                           imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Also, use the new syntax for accessing arrays. objectAtIndex is a very old syntax :)

You need to write this way

UIImage *image = [UIImage imageNamed:[self.menuCellImages objectAtIndex:indexPath.item] ];
cell.menuCellIcon.image =   [image
     imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

cell.menuCellIcon.tintColor = [UIColor whiteColor];

You do not use the UIImage that is returned from this line:

[cell.menuCellIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

You should use the following syntax:

cell.menuCellIcon.image = [UIImage
                          imageNamed: [self.menuCellImages objectAtIndex:indexPath.item]                             
                          imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM