From eb350ec12bf712c4e8938c377a72023edea916d4 Mon Sep 17 00:00:00 2001 From: SBiOSoftWhare Date: Thu, 28 Nov 2019 12:42:19 +0100 Subject: [PATCH] Add convenient method to UIButton to set a background color as an image. --- Riot/Categories/UIButton.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Riot/Categories/UIButton.swift b/Riot/Categories/UIButton.swift index 01c1095f4..0e3af78ef 100644 --- a/Riot/Categories/UIButton.swift +++ b/Riot/Categories/UIButton.swift @@ -29,4 +29,15 @@ extension UIButton { titleLabel.numberOfLines = 0 titleLabel.textAlignment = textAlignment } + + /// Set background color as an image. + /// Useful to automatically adjust highlighted background if `adjustsImageWhenHighlighted` property is set to true or disabled background when `adjustsImageWhenDisabled`is set to true. + /// + /// - Parameters: + /// - color: The background color to set as an image. + /// - state: The control state for wich to apply this color. + func vc_setBackgroundColor(_ color: UIColor, for state: UIControl.State) { + let image = UIImage.vc_image(from: color) + self.setBackgroundImage(image, for: state) + } }