How to change tintColor of image in UIButton with imageEdgeInsets?(如何使用 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,但是当我尝试更改时,我的代码不起作用:
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];
我有这个:
我想得到它:
如何更改 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 属性更改为 Template Image.
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];
希望对你有所帮助.
这篇关于如何使用 imageEdgeInsets 更改 UIButton 中图像的 tintColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 imageEdgeInsets 更改 UIButton 中图像的 tintColor?
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
