-1

I tried to show map icons in UIAlertController, but when image do not have transparent background I will get fill gray color

   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)

but I get

image

its not original image color, how can I fix that ?

Andrew
  • 26,706
  • 9
  • 85
  • 101
  • I'm not 100% sure but, this could be related to the bug mentioned in [this answer](https://stackoverflow.com/a/37737212/2180785) – Frakcool Nov 20 '19 at 17:51
  • @Frakcool how can I apply answer for this question ? I need transparent tint for image and still default tint for another vc –  Nov 20 '19 at 17:55
  • Idk, I found that related question and thought it might be useful for you, give it a try and if it works, then we could come up with a workaround – Frakcool Nov 20 '19 at 17:57

3 Answers3

1

To make tint color of the button's image, the image should be render as Template :

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

  • from the assets folder:enter image description here

Watercayman
  • 7,970
  • 10
  • 31
  • 49
Magy Elias
  • 31
  • 4
0

try

yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysTemplate), for: .normal)
OldTimes
  • 300
  • 1
  • 3
  • 16
0

What I use is

yandexBtn.setImage(UIImage(named: "yandex")?.withRenderingMode(.alwaysOriginal), for: .normal)
Sam
  • 1,765
  • 11
  • 82
  • 176