更改按钮中png的颜色-iOS [英] Change color of png in buttons - ios

查看:219
本文介绍了更改按钮中png的颜色-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一组透明的白色PNG图标:

I've got a set of icons that I've created that are transparent white PNGs:

我想做的就是能够将它们着色为其他颜色.例如蓝色,灰色等.

And what I'd like to do is be able to tint them to other colors. Such as blue, grey, etc.

我注意到,单击/点击"它们会自动变为灰色.因此,我想我可以通过点击或将其正常状态更改为我喜欢的灰色:

I've noticed that 'clicked/tapped' they change automatically to a grey. So I assume I can change that grey to whatever I like either with a tap or its normal state:

实现此目标的最佳方法是什么?

What would be the best way to achieve this?

推荐答案

以下代码将为按钮的正常状态设置颜色:

Following code will set tint colour for normal state of button:

对于Swift 4和更高版本:

For Swift 4 and newer:

let origImage = UIImage(named: "imageName")
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
btn.setImage(tintedImage, for: .normal)
btn.tintColor = .red

当按钮的状态更改时,您可以根据需要更改颜色.

You can change tint colour according to your need when state changes for button.

对于Swift 3:

let origImage = UIImage(named: "imageName")
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
btn.setImage(tintedImage, forState: .normal)
btn.tintColor = .redColor

对于Swift 2: 查看修订历史记录.

For Swift 2: see revision history.

这篇关于更改按钮中png的颜色-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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