27

我有一个创建自定义 UIButton 的方法,它允许我使用 QuartzCore 更改按钮的颜色。但是按钮在触摸时不会突出显示。

- (UIButton *)makeHeaderButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIFont *textFont = [UIFont boldSystemFontOfSize:12];
    UIColor *textColor = [UIColor colorWithRed:80/255.0 green:109/255.0 blue:145/255.0 alpha:1.0];
    UIColor *backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1.0];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    button.titleLabel.font = textFont;
    button.autoresizesSubviews = YES;
    button.layer.cornerRadius = 8;
    button.layer.borderWidth = 1;
    // next 2 properties set using QuartzCore class, no other way to set button colors
    button.layer.borderColor = [UIColor grayColor].CGColor;
    button.layer.backgroundColor = backgroundColor.CGColor;
    button.clipsToBounds = YES;
    return button;
}

如何使这些按钮像常规圆形矩形按钮一样突出显示?

4

7 回答 7

25

在 Swift 中,您还可以覆盖isHighlightedvar 并在其上添加 alpha 动画。

override var isHighlighted: Bool {
    didSet {
        UIView.animate(withDuration: 0.25, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction], animations: {
            self.alpha = self.isHighlighted ? 0.5 : 1
        }, completion: nil)
    }
}
于 2018-04-24T08:46:25.713 回答
13

在您的代码中,添加该行button.showsTouchWhenHighlighted = TRUE;

于 2012-12-30T03:02:35.790 回答
6

如果有人仍然遇到问题 - 您应该像这样创建 UIButton:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.layer.cornerRadius = 4;
btn.layer.masksToBounds = YES;

[btn setTranslatesAutoresizingMaskIntoConstraints:NO];

[btn setBackgroundColor:[UIColor greenColor]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[btn setTitle:@"ok" forState:UIControlStateNormal];

*** any customisation that you would like ***

[parentView addSubview: btn];

所以这样你仍然有 iOS 来处理所有的突出显示。主要是使用

[UIButton buttonWithType:UIButtonTypeSystem];

如果您使用 initWithFrame 或任何其他方法 - 您必须实施

setTitleColor: forState: 
setBackgroundImage: forState:
于 2015-07-21T11:21:55.363 回答
6

对于Swift,创建一个继承UIButton并覆盖isHighlighted属性的自定义类:

class ButtonWithHighlight: UIButton {

    override var isHighlighted: Bool {
        get {
            return super.isHighlighted
        }
        set {
            if newValue {
                backgroundColor = <#color when highlighted#>
            }
            else {
                backgroundColor = <#color for normal state#>
            }
            super.isHighlighted = newValue
        }
    }

}
于 2017-11-09T21:27:53.713 回答
4

您可以创建自己的子类UIButton(然后您只需在构造函数中设置所有这些属性)。

而且您必须覆盖的 highlight 方法UIButton

- (void)setHighlighted:(BOOL)highlighted {
    if (highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor blueColor];
    }
}
于 2016-01-13T13:56:56.920 回答
-1

有一种简单的方法可以在自定义按钮上实现高亮。1.为高亮状态创建.png图像(带有颜色,alpha等);2.进入yourbutton.xib;3.选择XCode右侧的Attributes Inspector;4. 将状态配置设置为突出显示 5. 将您的 .png 图像设置为背景图像。不客气 :)

于 2015-05-30T17:37:39.880 回答
-6

在 Storyboard 中设置按钮背景默认图像和突出显示的图像。在按钮操作上写下这一行,即更改按钮颜色 1 秒

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
于 2014-11-13T08:28:18.460 回答