How can I color a UIImage in Swift?

后端 未结 20 2237
执念已碎
执念已碎 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 19:08

    Swift 3 extension wrapper from @Nikolai Ruhe answer.

    extension UIImageView {
    
        func maskWith(color: UIColor) {
            guard let tempImage = image?.withRenderingMode(.alwaysTemplate) else { return }
            image = tempImage
            tintColor = color
        }
    
    }
    

    It can be use for UIButton as well, e.g:

    button.imageView?.maskWith(color: .blue)
    

提交回复
热议问题