如何更改UIButton的高亮颜色?

我在我的UINavigationController中创build了一个导航button。 我将其设置为在被触摸时突出显示:

[someButton setShowsTouchWhenHighlighted:YES]; 

有没有办法将突出显示的颜色更改为默认的白色以外的东西?

尝试用下面的方法覆盖UIButton ..只是当它处于高亮状态时,更改button的背景颜色。

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

尝试一下..希望它有帮助

您可以使用setBackgroundImage:forState:突出显示时设置button的背景图像。

例如:

正如Tim在他们的回答中所build议的那样,您可以从UIColor创build一个UIImage

 - (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的背景图像

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

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

如果您希望突出显示的颜色是原始背景颜色的深色版本,我将提供一个Swift答案,可以在不进行任何自定义的情况下使用。 另一方面,如果您需要完全不同的突出显示背景色,则可以将其作为UIColor提供给highlightBackgroundColor属性。

以下是在Swift中实现这种function的最有效和直接的方法。 这也是通用的,这意味着它可以用于许多不同颜色的不同button。

代码如下:

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

把这个button当作一个普通的UIButton,加上可选属性highlightBackgroundColor:

 let highlightedColorButton = HighlightedColorButton.buttonWithType(.Custom) as HighlightedColorButton highlightedColorButton.backgroundColor = UIColor.redColor() highlightedColorButton.highlightedBackgroundColor = UIColor.blueColor() 

使用此语句来设置UIButton的突出显示的颜色:

 [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 

在Swift中,如果你想使用高亮状态的图像,你可以用UIButton来做到这一点:

 if enabled { //highlighted state enableMicrophone.setImage(UIImage(named: "mic_on"), forState: .Highlighted) } else { //normal state enableMicrophone.setImage(UIImage(named: "mic_off"), forState: .Normal) } 

我通过使用旧的,旧的networking技巧获得了一些成功。

我在images.xcassets ,PNG文件中创build了几个图片资源,都是1px x 1px。 一个是正​​常的颜色,另一个是背景颜色。

我从纯黑色变成纯白色,所以:

在故事板中,我selectbutton。 在“属性检查器”(右侧面板,第四个图标)中,我将types更改为“自定义”,将“状态configuration”更改为“高亮”,然后将“背景”更改为“whitePixel”(或任何您称之为“ )和“文字颜色”为“黑色”。

现在我将“状态configuration”更改为“默认”,将“背景”更改为“blackPixel”(或其他),将“文本颜色”更改为“白色”。

我猜你可以使用这些像素,只要你只有访问故事板编辑器中的图像时需要应用颜色。

这确实增加了图像资产的数量,但却大大减less了所需的代码。 哪个让我喜欢。

对于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 } } 

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

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