【问题标题】:How to tint a button and then when you tap in it it doesn't change color?如何为按钮着色,然后当您点击它时它不会改变颜色?
【发布时间】:2020-11-21 03:17:45
【问题描述】:

我有一个黑色图标,我用它来制作图像按钮。我以编程方式将此按钮染成灰色。当屏幕显示时,按钮是灰色的,但是一旦我点击按钮,它又会变黑。

这就是我为按钮着色的方式:

addQuantityButton.imageView?.image = addQuantityButton.imageView?.image?.withRenderingMode(.alwaysTemplate)
addQuantityButton.imageView?.tintColor = .gray

谢谢!

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    请试试这个,它会解决你的问题。

    图像模式

       addQuantityButton.setImage(addQuantityButton.currentImage!.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
        addQuantityButton.tintColor = .gray
    

    背景图片模式

    addQuantityButton.setImage(addQuantityButton.currentBackgroundImage!.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
        addQuantityButton.tintColor =.gray
    

    【讨论】:

      【解决方案2】:

      试试这个:

      setImage(image.withRenderingMode(.alwaysTemplate), for: .highlighted)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        相关资源
        最近更新 更多