개발

swift] UIbutton의 UIimage 색상변경하기

dev.jake 2021. 8. 4. 10:33

https://stackoverflow.com/questions/27163171/change-color-of-png-in-buttons-ios

 

Change color of png in buttons - ios

I've got a set of icons that I've created that are transparent white PNGs: And what I'd like to do is be able to tint them to other colors. Such as blue, grey, etc. I've noticed that 'clicked/ta...

stackoverflow.com

 

btn.setImage(name: "testImage",  for: .normal)

btn.setImageTintColor(UIColor.red)


extension UIButton{

 

    func setImageTintColor(_ color: UIColor) {

        let tintedImage = self.imageView?.image?.withRenderingMode(.alwaysTemplate)

        self.setImage(tintedImage, for: .normal)

        self.tintColor = color

    }

 

}