更改按钮的PDF矢量图像的tintColor

Yos*_*far 4 ios swift swift5 xcode11.1

我已将PDF图像作为资产添加到Xcode,我想更改tintColor图像的 ,但没有成功。

我尝试添加User Defined Runtime Attributes,但它不起作用。

也尝试过以编程方式更改,但行不通。

self.buttonBringFriend.imageView.tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

有没有人有办法解决吗?

Xcode 11.1 斯威夫特 5.1

Jul*_*ian 7

Render As: Template Image在Asset属性中设置了吗?

在此输入图像描述


pkc*_*456 7

要设置图像颜色,请使用以下方法:

extension UIImageView {

    func setImageColor(color: UIColor) {
            let templateImage = self.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
            self.image = templateImage
            self.tintColor = color
        }

    }
Run Code Online (Sandbox Code Playgroud)

如何使用:-

self.buttonBringFriend.imageView.setImageColor(color: .white)
Run Code Online (Sandbox Code Playgroud)