UIImageView で Tint カラーを使用する 質問する

UIImageView で Tint カラーを使用する 質問する

独自の のサブクラスがありますUIButtonUIImageViewこれに を追加して画像を追加します。 色付きの色で画像の上にペイントしたいのですが、うまくいきません。

これまでのところ、次のとおりです。

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

何が間違っていたのでしょうか? (画像の色は常に元の色のままです。)

ベストアンサー1

このコードの代わりに:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

次のものが必要です:

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

これを使用スウィフト4.1

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

おすすめ記事