I\'ve a UIButton
and I\'ve created an extension to add background color for different state.
I\'m using the following code:
Swift 5, IOS 13+
extension UIButton {
func setBackgroundColor(_ color: UIColor, for forState: UIControl.State) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: forState)
}
}