【问题标题】:Change ImageView's tint color in UIButton based on state根据状态更改 UIButton 中 ImageView 的 tint 颜色
【发布时间】:2019-07-17 04:22:59
【问题描述】:

我在情节提要中有一个UIButton,它只显示图像而不显示文本。它的类型是系统。点击按钮时需要更改图像的色调颜色。我如何使用UIButton's 状态配置来做到这一点?

我已尝试在代码中为不同状态使用setImage,如下所示,但无法使其正常工作。谢谢

setImage(UIImage(named: "test")?.applying(tintColor: .red), for: .highlighted)
setImage(UIImage(named: "test")?.applying(tintColor: .blue), for: .normal)

【问题讨论】:

    标签: ios swift xcode uibutton uiimageview


    【解决方案1】:

    检查以下代码:-

    @IBAction func onClickButton(_ sender: UIButton) {
     sender.imageView.image = sender.imageView.image?.withRenderingMode(.alwaysTemplate)
        if sender.isSelected || sender.isHighlighted{
            sender.imageView.tintColor = .red
        }else{
             sender.imageView.tintColor = .blue
        }
    }
    

    【讨论】:

      【解决方案2】:
       Follow the below steps plus your already done work:
      1) Go to Images.xcassets and select your image i.e test
      2) Click on attribute inspector on the right hand side of xcode
      3) Change Render as option from "Default" to "Template Image"
      4) Run again and test
      

      详情请看图片

      【讨论】:

      • 这也有效,但我更愿意在代码中做同样的事情。谢谢
      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多