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

时间:2023-01-22 20:55:04

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?

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

7 个解决方案

#1


13  

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

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

#2


5  

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:

#3


2  

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

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

And you must override the highlight method of UIButton

并且您必须覆盖UIButton的突出显示方法

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

#4


2  

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

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

#5


1  

For Swift, create a custom class that inherits from UIButton and override isHighlighted property:

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

}

#6


-1  

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.转到yourbutton.xib; 3.选择XCode右侧的Attributes Inspector; 4.将状态配置设置为突出显示5.将.png图像设置为背景图像。不用谢 :)

#7


-4  

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]];

#1


13  

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

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

#2


5  

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:

#3


2  

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

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

And you must override the highlight method of UIButton

并且您必须覆盖UIButton的突出显示方法

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

#4


2  

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

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

#5


1  

For Swift, create a custom class that inherits from UIButton and override isHighlighted property:

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

}

#6


-1  

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.转到yourbutton.xib; 3.选择XCode右侧的Attributes Inspector; 4.将状态配置设置为突出显示5.将.png图像设置为背景图像。不用谢 :)

#7


-4  

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]];