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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14087725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:39:59  来源:igfitidea点击:

Custom UIButton not highlighting when touched

iosxcodeuibutton

提问by Bob

I have a method that creates a custom UIButton that allows me to change the color of the button using QuartzCore. But the buttons don't highlight when touched.

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

How can I make these buttons highlight like a regular round rect button?

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

回答by GrizzlyBear

In Swift, you can also override the isHighlightedvar and add an alpha animation on it.

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

回答by Garrett

In your code, add the line button.showsTouchWhenHighlighted = TRUE;

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

回答by quarezz

If someone still encounters the problem - you should create UIButton like this:

如果有人仍然遇到问题 - 你应该像这样创建 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];

So this way you still have iOS taking care of all highlighting. The main thing is using

因此,通过这种方式,您仍然可以让 iOS 处理所有突出显示。主要是使用

[UIButton buttonWithType:UIButtonTypeSystem];

If you use initWithFrame or any other method - you would have to implement

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

setTitleColor: forState: 
setBackgroundImage: forState:

回答by jvrmed

For Swift, create a custom class that inherits from UIButtonand override isHighlightedproperty:

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

}

回答by KIDdAe

You can create your own subclass of UIButton(then you simply set all thoses properties in the constructor).

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

And you must override the highlight method of UIButton

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

- (void)setHighlighted:(BOOL)highlighted {
    if (highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor blueColor];
    }
}

回答by Vanja Dev

There is easy way to implement highlight on custom button. 1. create .png image for highlight state (with color, alpha, etc.); 2. go to yourbutton.xib; 3. select Attributes Inspector on the right of XCode; 4. set State Config as Highlighted 5. set you .png image as Background image. You are welcome :)

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

回答by iosBeginner

In Storyboard set the button background default image and highlighted image. on Button action write this line i.e. change the button colour for 1 second

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

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