简体   繁体   English

图像的色彩颜色不变

[英]Tint color of image not changing

I have a menu bar made with collection view. 我有一个用集合视图制作的菜单栏。 Each cell contains a image (png format) 每个单元格包含一张图片(png格式)

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]; 这行... [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 :) objectAtIndex是一种非常古老的语法:)

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: 您不使用从以下行返回的UIImage

[cell.menuCellIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

You should use the following syntax: 您应该使用以下语法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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