Ios 更改按钮的颜色';s PDF矢量图像
我已将Ios 更改按钮的颜色';s PDF矢量图像,ios,swift,swift5.1,xcode11.1,Ios,Swift,Swift5.1,Xcode11.1,我已将PDF图像作为资产添加到Xcode,我想更改图像的tintColor,但没有成功 我已尝试添加用户定义的运行时属性,但它不起作用 self.buttonBringFriend.imageView.tintColor = UIColor.white 也尝试按程序进行更改,但不起作用 self.buttonBringFriend.imageView.tintColor = UIColor.white 有人有解决办法吗 Xcode 11.1 Swift 5.1如果您在资产属性中设置了渲染为:
PDF
图像作为资产添加到Xcode
,我想更改图像的tintColor
,但没有成功
我已尝试添加用户定义的运行时属性
,但它不起作用
self.buttonBringFriend.imageView.tintColor = UIColor.white
也尝试按程序进行更改,但不起作用
self.buttonBringFriend.imageView.tintColor = UIColor.white
有人有解决办法吗
Xcode 11.1 Swift 5.1如果您在资产属性中设置了
渲染为:模板图像
要设置图像颜色,请使用以下方法:
extension UIImageView {
func setImageColor(color: UIColor) {
let templateImage = self.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
self.image = templateImage
self.tintColor = color
}
}
如何使用:-
self.buttonBringFriend.imageView.setImageColor(color: .white)
效果很好,但当我单击图像并将其高亮显示时,颜色会变回原始颜色。@YossiTsafar在高亮显示时,使用此方法设置相同的图像。有两种方法可以做到这一点:1)对按钮进行子类化,并在突出显示时调用此方法。2) 设置相同的高亮显示图像已尝试过,但它不起作用。我要调用self.highlightedImage吗?@YossiTsafar,是的,在
setImageColor
方法中,写这行:self。highlightedImage=templateImage
设置颜色后,尝试添加此行:buttonBringFriend.imageView?.highlightedImage=buttonBringFriend.imageView?.image