How to change the highlighted color of a UIButton?

I created a navigation button in my UINavigationController. I set it to highlight when touched:

[someButton setShowsTouchWhenHighlighted:YES]; 

Is there a way to change the highlighted color to something other than white?

+33
ios objective-c iphone uibutton
Nov 30 '13 at 13:23
source share
8 answers

Try overriding the UIButton with the following method .. and just change the color of the button's backgroud button when it is in the selected state.

 - (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { self.backgroundColor = [UIColor Your Customcolor]; } } 

Try it ... hope this helps

+47
Nov 30 '13 at 13:35
source share

You can use setBackgroundImage:forState: to set the background image for the button when highlighted.

ex:

As suggested by Tim in their answer here , you can create a UIImage from UIColor :

 - (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; } 

Then set the image as the background image of the button, if highlighted

[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];

+41
Nov 30 '13 at 18:13
source share

In Swift :

 import UIKit class CustomUIButtonForUIToolbar: UIButton { // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code super.drawRect(rect) self.layer.borderColor = UIColor.blueColor().CGColor self.layer.borderWidth = 1.0 self.layer.cornerRadius = 5.0 self.clipsToBounds = true self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal) self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted) } override var highlighted: Bool { didSet { if (highlighted) { self.backgroundColor = UIColor.blueColor() } else { self.backgroundColor = UIColor.clearColor() } } } } 
+19
Nov 22 '14 at 17:27
source share

I provided a Swift answer that you can use without any configuration if you want the highlighted color to be a darkened version of the original background color. If you, on the other hand, want a completely different highlighted background color, you can provide this highlightBackgroundColor property as UIColor.

Below is the most efficient and easiest way to implement such a function in Swift. It is also general, that is, it can be used for many different buttons with different colors.

Here is the code:

 import UIKit class HighlightedColorButton: UIButton { // A new highlightedBackgroundColor, which shows on tap var highlightedBackgroundColor: UIColor? // A temporary background color property, which stores the original color while the button is highlighted var temporaryBackgroundColor: UIColor? // Darken a color func darkenColor(color: UIColor) -> UIColor { var red = CGFloat(), green = CGFloat(), blue = CGFloat(), alpha = CGFloat() color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) red = max(red - 0.5, 0.0) green = max(green - 0.5, 0.0) blue = max(blue - 0.5, 0.0) return UIColor(red: red, green: green, blue: blue, alpha: alpha) } // Set up a property observer for the highlighted property, so the color can be changed @objc override var highlighted: Bool { didSet { if highlighted { if temporaryBackgroundColor == nil { if backgroundColor != nil { if let highlightedColor = highlightedBackgroundColor { temporaryBackgroundColor = backgroundColor backgroundColor = highlightedColor } else { temporaryBackgroundColor = backgroundColor backgroundColor = darkenColor(temporaryBackgroundColor!) } } } } else { if let temporaryColor = temporaryBackgroundColor { backgroundColor = temporaryColor temporaryBackgroundColor = nil } } } } } 

Treat the button as a regular UIButton by adding the additional highlightBackgroundColor property:

 let highlightedColorButton = HighlightedColorButton.buttonWithType(.Custom) as HighlightedColorButton highlightedColorButton.backgroundColor = UIColor.redColor() highlightedColorButton.highlightedBackgroundColor = UIColor.blueColor() 
+4
Nov 22 '14 at 22:53
source share

Use this statement to set the highlighted UIButton color:

 [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 
+3
Nov 30 '13 at 13:35
source share

In Swift, if you want to use an image for the selected state, you can do this with UIButton :

 if enabled { //highlighted state enableMicrophone.setImage(UIImage(named: "mic_on"), forState: .Highlighted) } else { //normal state enableMicrophone.setImage(UIImage(named: "mic_off"), forState: .Normal) } 
+1
Jun 04 '15 at 20:12
source share

I am getting some success using an old old trick.

I created a couple of images in the images.xcassets , PNG, and 1px by 1px files. One is the usual color, the other is the background color.

I go from pure black to pure white, like this:

In the storyboard, I select the button. In the "Attributes Inspector" (right panel, fourth icon across) I change the type to "custom", change the "state configuration" to "highlight", then the "background" to "whitePixel" (or as you called it)) and " Text color to black.

Now I change the “state configuration” to “default”, “background” to “black pixel” (or something else) and “text color” to “white color”.

I think you can use these pixels whenever you need to apply color, when you only have access to images in the storyboard editor.

This increases the number of image objects, but significantly reduces the required code. What makes me happy.

0
May 20 '15 at
source share

For swift

 import UIKit extension UIImage { func imageWithAlphaComponent(alpha: CGFloat) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, 0) let ctx = UIGraphicsGetCurrentContext() let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) CGContextScaleCTM(ctx, 1, -1) CGContextTranslateCTM(ctx, 0, -area.size.height) CGContextSetBlendMode(ctx, CGBlendMode.Multiply) CGContextSetAlpha(ctx, alpha) CGContextDrawImage(ctx, area, self.CGImage) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } } 

Record

 button.setImage(image.imageWithAlphaComponent(0.65), forState: .Highlighted) 

https://gist.github.com/yukitoto/e8aa420e20be7ab5d64b71e96ecd61f1

0
Oct 08 '16 at 12:11
source share



All Articles