Ios 自定义按钮在触摸时不突出显示

Ios 自定义按钮在触摸时不突出显示,ios,xcode,uibutton,Ios,Xcode,Uibutton,我有一个创建自定义UIButton的方法,它允许我使用QuartzCore更改按钮的颜色。但是按钮在触摸时不会突出显示 - (UIButton *)makeHeaderButton { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; UIFont *textFont = [UIFont boldSystemFontOfSize:12]; UIColor *textColor = [UICol

我有一个创建自定义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;
}

如何使这些按钮像普通的圆形矩形按钮一样高亮显示?

在代码中,添加行
按钮。showsTouchWhenHighlighted=TRUE

在序列图像板中设置按钮背景默认图像和高亮显示图像。 在按钮操作上写下这一行,即更改按钮颜色1秒

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];

有一种简单的方法可以实现自定义按钮上的高亮显示。 1.为高亮状态创建.png图像(带有颜色、alpha等); 2.转到yourbutton.xib; 3.选择XCode右侧的属性检查器; 4.将状态配置设置为高亮显示 5.将.png图像设置为背景图像。
欢迎您:)

如果有人仍然遇到问题,您应该创建如下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:

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

并且您必须覆盖
ui按钮的高亮显示方法

- (void)setHighlighted:(BOOL)highlighted {
    if (highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor blueColor];
    }
}
对于Swift,创建一个自定义类,该类继承自
UIButton
并重写
ishighlight
属性:

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

}
class按钮带高亮显示:ui按钮{
覆盖变量isHighlighted:Bool{
得到{
返回super.ishighlight
}
设置{
如果是新值{
背景色=
}
否则{
背景色=
}
super.ishighlight=newValue
}
}
}

在Swift中,您还可以覆盖
isHighlighted
变量并在其上添加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)
    }
}

这似乎只是使标题淡入淡出。我希望当触摸按钮时,按钮背景会闪烁成蓝色。@Bob您可以随时设置手动高亮显示。例如:注册一个事件
onTouchDown
(或任何它是什么),然后在实现中执行类似于
button.layer.backgroundColor=[UIColor BlueColor]
然后
ontouch
将颜色更改回您的设置哇,这越来越复杂了。我只想做一个带有自定义背景色的RoundRect按钮。当然有一种更简单的方法可以做到这一点。我用我想要的背景色制作了一个png文件,然后使用setBackgroundImage:forState:更改roundRect按钮的颜色。@Bob很难相信没有办法将按钮背景色设置为高亮状态:(使用系统按钮与自定义按钮是关键。即使按钮是在故事板或Nib上创建的。在IB中,在按钮上设置图像会将其更改为自定义按钮,因此您需要手动将其更改回。当您点击并按住
ishighlight
时,会生成大量重复事件。因此,最好先检查值是否为更改:
guard oldValue!=self.ishighted else{return}
操作alpha而不是backgroundColor似乎有效better@AntonTropashko如果高亮显示的颜色与正常状态的颜色相同,这可能会更好,但在需要白色正常按钮和绿色高亮显示按钮的情况下,它不起作用