我有一个UIImage,是一个全黑的小符号.UIImage是在自定义UIButton子类中设置的.有没有可能让图像将tintColor应用于它,因此它会将 colored颜色 更改为tintColor是什么,而不是黑色图像?

我只是想避免创造新的assets资源 .

// here I want defaultImageName (that is black) to use the tintColor (that is white)
[self setImage:[UIImage imageNamed:defaultImageName] forState:UIControlStateNormal];

推荐答案

如果你只是支持iOS 7,你可以使用tintColorUIImageRenderingModeAlwaysTemplate

本文包括:

https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming

如果您需要支持早期版本,您可能需要考虑此线程.

How would I tint an image programmatically on the iPhone?

Objective-c相关问答推荐

[NSNull new]和[NSNull NULL]有什么不同?

通过指针算术访问数组值与 C 中的下标

如何使用 UIStepper

MKMapView zoom 到 viewDidLoad 上的用户位置?

将 HEX NSString 转换为 NSData

iOS 应用 Display Recorder 如何在不使用私有 API 的情况下录制屏幕?

定义缓存变量时在objective-c中使用static关键字

使用 UIScrollView 用两根手指滚动

iOS中CALayer的UIImage

如何使用整数值作为键在 NSMutableDictionary 中设置值?

右对齐的 UITextField 空格键在 iOS 7 中不前进光标

Objective-C 的 Eclipse 插件?

在 iOS 和 Mac OS X 应用程序中使用 C++

iOS框架改变一个属性(例如宽度)

如何切换回已经加载的 UIViewController?

添加自定义 initWith?

以流畅的动画显示/隐藏导航栏

如何以编程方式移动 UIScrollView 以集中在键盘上方的控件中?

NSMutableArray 按顺序添加对象

为什么 LLDB 不能打印 view.bounds?