如何更改UIButton的突出显示颜色?

时间:2013-11-30 13:23:35

标签: ios iphone objective-c uibutton

我在UINavigationController中创建了一个导航按钮。 我将其设置为在触摸时突出显示:

[someButton setShowsTouchWhenHighlighted:YES];

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

8 个答案:

答案 0 :(得分:49)

尝试使用以下方法覆盖UIButton ..只需更改按钮处于突出显示状态时的背景颜色。

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = [UIColor Your Customcolor];
    }
    else{
        self.backgroundColor = [UIColor Your DefaultColor];
    }   

}

试试吧..希望它有帮助

答案 1 :(得分:44)

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

前:

正如Tim在their answer here中建议的那样,您可以从UIImage创建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;
}

然后在突出显示时将图像设置为按钮的背景图像

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

答案 2 :(得分:24)

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

        }
    }

}

答案 3 :(得分:4)

如果您希望突出显示的颜色是原始背景颜色的黑暗版本,我将提供 Swift 答案,无需任何自定义即可使用。另一方面,如果您想要一个完全不同的突出显示背景颜色,您可以将它作为UIColor提供给highlightedBackgroundColor属性。

以下是在Swift中实现此类功能的最有效和直接的方法。它也是通用的,这意味着它可以用于许多不同颜色的不同按钮。

以下是代码:

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

将按钮视为普通的UIButton,并添加了可选属性highlightBackgroundColor:

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

答案 4 :(得分:2)

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

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

答案 5 :(得分:1)

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

答案 6 :(得分:1)

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

答案 7 :(得分:0)

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

我在images.xcassets,PNG文件中创建了几个图像资源,均为1px xpx。一种是正常颜色,另一种是背景颜色。

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

在故事板中,我选择按钮。在“属性”检查器中' (右侧面板,第四个图标)我将类型更改为' custom',更改'状态配置'突出'然后'背景'到了

现在我改变了状态配置'到'默认''背景' to' blackPixel' (或其他)和“文本颜色”'到了白色'。

我想你只需要在Storyboard编辑器中访问图像时需要应用颜色就可以使用这些像素。

这确实增加了图像资源的数量,但却极大地减少了所需的代码。这让我高兴。