ios 更改 UIButton 的按下颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17227844/
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-31 00:00:25  来源:igfitidea点击:

Change pressed color of UIButton

iosuibutton

提问by Sandy

Is there any way to change the color of a UIButton when it's pressed?

有没有办法在按下时更改 UIButton 的颜色?

The default seems to be a blue color, but I don't see where or how to change this to a custom color.

默认似乎是蓝色,但我不知道在哪里或如何将其更改为自定义颜色。

Thanks!

谢谢!

采纳答案by Sandy

I ultimately followed the following poster's suggestion. It worked perfectly.

我最终遵循了以下海报的建议。它工作得很好。

https://stackoverflow.com/a/10670141/720175

https://stackoverflow.com/a/10670141/720175

In my particular case I created a subclass of UIButton, with the final code as:

在我的特殊情况下,我创建了一个 UIButton 的子类,最终代码为:

-(void) setHighlighted:(BOOL)highlighted
{
    if(highlighted) {
        self.backgroundColor = [UIColor colorWithRed:1 green:0.643 blue:0.282 alpha:1];
    } else {
        self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    }
    [super setHighlighted:highlighted];
}

Easy as pie.

非常简单。

回答by Geek

If you are using storyboard then in inspector window you can change the highlight tintproperty to the color you want on button click event.

highlight tint

Look at the Highlight Tintproperty in image.

如果您使用的是故事板,那么在检查器窗口中,您可以将highlight tint属性更改为按钮单击事件所需的颜色。

突出色调

查看Highlight Tint图像中的属性。