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
Change pressed color of UIButton
提问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 tint
property to the color you want on button click event.
Look at the Highlight Tint
property in image.
如果您使用的是故事板,那么在检查器窗口中,您可以将highlight tint
属性更改为按钮单击事件所需的颜色。
查看Highlight Tint
图像中的属性。