如何用imageEdgeInsets更改UIButton中图像的tintColor? [英] How to change tintColor of image in UIButton with imageEdgeInsets?

查看:186
本文介绍了如何用imageEdgeInsets更改UIButton中图像的tintColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-c应用程序中有一个UIButton。我的按钮被修改为添加文本和图像,如:

I I have an UIButton in my objective-c application. My button was modified adding text and image like:

- (void)centerButtonAndImageWithSpacing:(CGFloat)spacing {
     CGFloat insetAmount = spacing / 2.0;
     self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount);
     self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
     self.contentEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, insetAmount);
}

然后我用这段代码添加图片:

And then I add the image with this code:

[self.myButton setImage:[UIImage imageNamed:@"imageButton.png"] forState:UIControlStateNormal];

我想更改图片的tintColor,但是当我尝试更改它时,我的代码不是work:

I want change the tintColor of the image, but when I try change this, my code isn't work:

[self.myButton setTintColor:[UIColor redColor]];

我正在尝试使用此代码,但无效:

I'm trying use this code, but didn't work:

UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor redColor];

我有:

我想得到它:

I want get it:

如何更改imageButton的颜色?

How could I change the color of the imageButton?

推荐答案

我们可以通过以下方式为透明图标应用所需的颜色。

We can apply the desired color for the transparent icons with the following way.

1.在 Assets.xcassets 上拖动图像,在资产属性检查器中将Render as属性更改为模板图片

1.Drag the image on Assets.xcassets and In asset attribute inspector change the Render as attribute to Template Image.

2.并设置所需的色调喜欢以下方式。

2.And set the required tint color like below way.

icon.tintColor = [UIColor redColor];

希望它能为您提供帮助。

Hope it will help you.

这篇关于如何用imageEdgeInsets更改UIButton中图像的tintColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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