aah*_*ens 51 objective-c uibutton uiimage ios
我有一个UIImage全黑的小符号.将UIImage在自定义设置得到UIButton的子类我.是否可以将图像应用tintColor到图像上,而不是将黑色图像改变为任何颜色tintColor?
我只是想避免创建新资产.
// here I want defaultImageName (that is black) to use the tintColor (that is white)
[self setImage:[UIImage imageNamed:defaultImageName] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
mad*_*ik3 70
如果你只是支持iOS 7,你可以使用tintColor和UIImageRenderingModeAlwaysTemplate
本文涵盖:
https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
如果您需要支持早期版本,您可能需要考虑此主题
zgo*_*ski 11
Swift 4,复制粘贴解决方案
@IBOutlet weak var iconImageView: UIImageView!
iconImageView.image = UIImage(imageLiteralResourceName: "myImageName").withRenderingMode(.alwaysTemplate)
iconImageView.tintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)
尝试这个:
func tinted(with color: UIColor) -> UIImage? {
defer { UIGraphicsEndImageContext() }
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
color.set()
self.withRenderingMode(.alwaysTemplate).draw(in: CGRect(origin: .zero, size: self.size))
return UIGraphicsGetImageFromCurrentImageContext()
}
Run Code Online (Sandbox Code Playgroud)
例如:
button.setImage(UIImage(systemName: "checkmark.circle")?.tinted(with: .systemGray), for: .normal)
Run Code Online (Sandbox Code Playgroud)
在 iOS 13+ 上,您可以使用以下内容:
UIImage(named: "img_name")?.withTintColor(.red)
Run Code Online (Sandbox Code Playgroud)
https://developer.apple.com/documentation/uikit/uiimage/3327300-withtintcolor
| 归档时间: |
|
| 查看次数: |
78503 次 |
| 最近记录: |