【问题标题】:Swift button image tint快速按钮图像色调
【发布时间】:2020-03-16 12:21:05
【问题描述】:

我尝试在 UIAlertController 中显示地图图标,但是当图像没有透明背景时,我会得到填充灰色

   let customView = UIView()
   // customView.translatesAutoresizingMaskIntoConstraints = false
    customView.backgroundColor = .clear

    customView.addConstraint(NSLayoutConstraint(item: customView,
                                                attribute: .height,
                                                relatedBy: .equal,
                                                toItem: nil,
                                                attribute: .notAnAttribute,
                                                multiplier: 1,
                                                constant: 100))


    var marginBtns:CGFloat = 10

    if yandexMap {
        let yandexBtn = UIButton()
        yandexBtn.frame = CGRect(x: marginBtns, y: margin, width: 30, height: 30)
        yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysOriginal), for: .normal)
        yandexBtn.sizeToFit()
        yandexBtn.imageView!.layer.cornerRadius = 5
        urlYandexAction = urlYandex
        yandexBtn.addTarget(self, action: #selector(actionYandexBtn), for: .touchUpInside)
        marginBtns += 70
        customView.addSubview(yandexBtn)
        //alert.addAction(alertYandex)
    }
    ...
    let alert = UIAlertController(title: "Open in",
                                  customView: customView,
                                  fallbackMessage: "",
                                  preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    present(alert, animated: true)

但我明白了

它不是原始图像颜色,我该如何解决?

【问题讨论】:

  • 我不是 100% 确定,但这可能与 this answer 中提到的错误有关
  • @Frakcool 我如何为这个问题申请答案?我需要图像的透明色调和另一个 vc 的默认色调
  • Idk,我发现了这个相关问题,并认为它可能对你有用,试一试,如果它有效,那么我们可以想出一个解决方法

标签: swift


【解决方案1】:

要使按钮图像着色,图像应呈现为模板

  • 通过编码: yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysTemplate), for: .normal)

  • 来自资产文件夹:

【讨论】:

    【解决方案2】:

    我用的是

    yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysOriginal), for: .normal)
    

    【讨论】:

      【解决方案3】:

      试试

      yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysTemplate), for: .normal)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        相关资源
        最近更新 更多