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

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

问题描述

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

到目前为止我有:

- (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 I did wrong? (color of image stays always the same-as it was original)

推荐答案

而不是此代码:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

你应该:

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Swift 4.1

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

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

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