How do I set UIButton background color forState: UIControlState.Highlighted in Swift
If anyone stops by, another way to go maybe more easily if it is something you need more than once... I wrote a short extension for UIButton, it works just fine:
for Swift 3
extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, forState: forState)
}
}
for Swift 4
extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControl.State) {
self.clipsToBounds = true // add this to maintain corner radius
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(color.cgColor)
context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: forState)
}
}
}
You use it just like setBackgroundImage
:
yourButton.setBackgroundColor(color: UIColor.white, forState: UIControl.State.highlighted)
UIButton background color for highlighted/selected state issue
I found the issue, UIButton
was set to System.I simply changed it to Custom, it started working as expected.
How to change the background color of a UIButton while it's highlighted?
You can override UIButton
's setHighlighted
method.
Objective-C
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = UIColorFromRGB(0x387038);
} else {
self.backgroundColor = UIColorFromRGB(0x5bb75b);
}
}
Swift 3.0 and Swift 4.1
override open var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? UIColor.black : UIColor.white
}
}
How to change the highlighted color of a UIButton?
Try to Override the UIButton with the following Method.. and just change the backgroud color of button when its in highlighted state.
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = [UIColor Your Customcolor];
}
else{
self.backgroundColor = [UIColor Your DefaultColor];
}
}
Try it..hope it helps
How to set UIButton background color by state
Get an UIImage
from UIColor
, use that image as background image for the button.
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
[button setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateHighlighted];
Related Topics
Looping Through Nsattributedstring Attributes to Increase Font Size
Autolayout Link Two Uilabels to Have the Same Font Size
Xcode 6 Keeps Renaming My App's Directory in iOS8 Simulator After Each Run
Uitableview Inside Uiscrollview Not Receiving First Tap After Scrollling
How to Debug an iOS Extension (.Appex)
Synchronous Url Request on Swift 2
How to Run Nstimer in Background Beyond 180Sec in iOS 7
How to Add a Button to the Mkpointannotation
Storyboard View Elements Greyed Out
Send Messages Between iOS and Watchos with Watchconnectivity in Watchos2
Test Whether a Uiview Is in the Middle of Animation
Gamecenter Authentication in Landscape-Only App Throws Uiapplicationinvalidinterfaceorientation