สวิฟท์ 3 :
วิธีแก้ปัญหานี้อาจจะสบายถ้าคุณได้กำหนดภาพของคุณผ่านตัวสร้างส่วนต่อประสาน xCode แล้ว โดยทั่วไปคุณมีส่วนขยายหนึ่งภาพเพื่อทำให้สีเป็นสี:
extension UIImage {
    public func image(withTintColor color: UIColor) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(CGBlendMode.normal)
        let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context.clip(to: rect, mask: self.cgImage!)
        color.setFill()
        context.fill(rect)
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
}
จากนั้นคุณสามารถจัดเตรียมส่วนขยายUIButtonนี้เพื่อปรับสีภาพสำหรับสถานะเฉพาะ:
extension UIButton {
    func imageWith(color:UIColor, for: UIControlState) {
        if let imageForState = self.image(for: state) {
            self.image(for: .normal)?.withRenderingMode(.alwaysTemplate)
            let colorizedImage = imageForState.image(withTintColor: color)
            self.setImage(colorizedImage, for: state)
        }
    }
}
การใช้งาน:
myButton.imageWith(.red, for: .normal)
PS (ทำงานได้ดีในเซลล์ตารางคุณไม่จำเป็นต้องเรียกsetNeedDisplay()วิธีการเปลี่ยนสีทันทีเนื่องจากส่วนขยายUIImage ..