首页 > 解决方案 > 如何在swift上更改pdf图像颜色?

问题描述

我添加了一个按钮并设置了一个 pdf 图像。我需要更改 pdf 图像颜色。能做到吗?

标签: iosswift

解决方案


您可以使用此扩展方法设置图像颜色,

import UIKit

extension UIButton {

    public func setImage(color: UIColor, for state: UIControl.State) {
        let image = self.image(for: state)
        self.setImage(image?.withRenderingMode(.alwaysTemplate), for: state)
        self.tintColor = color
    }
}

用法

yourButton.setImage(color: .red, for: .normal)

推荐阅读