在 UIImageView 上使用 Tint 颜色 [英] Using Tint color on UIImageView

查看:21
本文介绍了在 UIImageView 上使用 Tint 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的 UIButton 子类.我在其上添加 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.

到目前为止我有:

- (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];
        ...

并在选择时:

- (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.)

推荐答案

代替这段代码:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

你应该有:

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Swift 4.1

image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)

这篇关于在 UIImageView 上使用 Tint 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆