【问题标题】:Changing tintColor for button's PDF vector image更改按钮的 PDF 矢量图像的 tintColor
【发布时间】:2020-02-26 04:18:13
【问题描述】:

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

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

也尝试以编程方式进行更改,但它不起作用。

self.buttonBringFriend.imageView.tintColor = UIColor.white

有人有解决办法吗?

Xcode 11.1 斯威夫特 5.1

【问题讨论】:

    标签: ios swift swift5.1 xcode11.1


    【解决方案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
    【解决方案2】:

    您是否在资产属性中设置了Render As: Template Image

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2012-03-25
      • 1970-01-01
      相关资源
      最近更新 更多