UIButton background color overlaps text on highlight
I believe the UIControlState you're actually looking for is Focused
rather than Highlighted
.
Here's an example of how to change the backgroundColor
and titleColor
of a UIButton
when it becomes focused. (Accomplished with the help of this answer):
import UIKit
class ViewController: UIViewController {
let myButton = UIButton(type: UIButtonType.Custom)
let myOtherButton = UIButton(type: UIButtonType.Custom)
override func viewDidLoad() {
super.viewDidLoad()
// Button we will change on focus
myButton.frame = CGRectMake(view.frame.midX - 300, view.frame.midY, 400, 100)
myButton.setTitle("myButton", forState: .Normal)
// Normal
myButton.backgroundColor = UIColor.whiteColor()
myButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
// Focused
myButton.setBackgroundImage(imageWithColor(UIColor.orangeColor()), forState: .Focused)
myButton.setTitleColor(UIColor.whiteColor(), forState: .Focused)
view.addSubview(myButton)
// Other button so we can change focus // Just for example
myOtherButton.frame = CGRectMake(view.frame.midX + 300, view.frame.midY, 400, 100)
myOtherButton.setTitle("myOtherButton", forState: .Normal)
// Normal
myOtherButton.backgroundColor = UIColor.whiteColor()
myOtherButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
// Focused
myOtherButton.setBackgroundImage(imageWithColor(UIColor.orangeColor()), forState: .Focused)
myOtherButton.setTitleColor(UIColor.whiteColor(), forState: .Focused)
view.addSubview(myOtherButton)
}
// Function to create a UIImage filled with a UIColor
func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
This example in action:
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 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
}
}
Another UIButton background color issue
If you are creating a button in nib you can change all the properties via nib itself.
Like this
OR
If you want to do it codewise since you are adding it via nib points to note
- No need for an initialization.since it is a property from IB.
- Just directly provide frame via IB or in code via
-setFrame:
method - To set color, you can use
-setBackgroundcolor:
method
So my .h
@property (nonatomic,strong) IBOutlet UIButton *uiLoginButton;
.m
[self.uiLoginButton setFrame:buttonRect];
[self.uiLoginButton setBackgroundColor:[UIColor redColor]];
Preventing button image highlight when selected swift 4
Change
UIButton(type: .system)
to
UIButton(type: .custom)
Now you have complete control over the look and behavior of the button.
Custom, Imageless UIButton title disappears
In an XIB, you can set the title for each of the different states by changing the State Config
and then setting the title for the corresponding state. In the following example, I have set the Default
state title to Title
and the Highlighted
state title to a single space, which makes it appear as blank in the app. Note however, that the preview only shows the Default
settings, and does not update for the different configurations (see images below).
Default State Title
Highlighted State Title
Related Topics
Kvo Listener Issues in Swift 4
Lldb for Swift: Access Computed Property or Perform Function Call in Type Summary Python Script
Decoding Different Type with and Without Array
Create Record Only If Parent Exists in Vapor Using Fluent
Wrong Image Orientation After Displaytransform Call
List Is Not Conforming to Encodable
How to Get Unsaferawpointer on The Swift Object
How to Update Nslayoutconstraint in a UItableviewcell of Dynamic Height
Why Are Implicitly Unwrapped Variables Now Printing Out as Some(...) in Swift 4.1
Uibutton Background Color Overlaps Text on Highlight
How to Create Nested Dictionary Elements in Swift
Why Cannot Read The Receipt Data for On-Device Validation
Reduce Float Precision Using Regexp in Swift
Root Class of All Classes in Swift
Is There a Simple Way to Test If You Match One of a Set of Enumerations
Force Refresh on Another Viewcontroller Component with Swift