ios – 自定义UIButton触摸时不突出显示

前端之家收集整理的这篇文章主要介绍了ios – 自定义UIButton触摸时不突出显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个方法创建一个自定义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;
}

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

解决方法

在你的代码中,添加一行button.showsTouchWhenHighlighted = TRUE;

猜你在找的iOS相关文章