How can I color a UIImage in Swift?

后端 未结 20 2273
执念已碎
执念已碎 2020-11-29 18:27

I have an image called arrowWhite. I want to colour this image to black.

func attachDropDownArrow() -> NSMutableAttributedString {
    let im         


        
20条回答
  •  一个人的身影
    2020-11-29 18:55

    Add this extension in your code and change image color in storyboard itself.

    Swift 4 & 5:

    extension UIImageView {
        @IBInspectable
        var changeColor: UIColor? {
            get {
                let color = UIColor(cgColor: layer.borderColor!);
                return color
            }
            set {
                let templateImage = self.image?.withRenderingMode(.alwaysTemplate)
                self.image = templateImage
                self.tintColor = newValue
            }
        }
    }
    

    Storyboard Preview:

提交回复
热议问题