ios Xcode - 单击按钮时更改按钮背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22361310/
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
Xcode - Change button background colour on Button click
提问by Greg
I know this might have been asked before but, I've been trying to find an answer to this for ages. I am new to Xcode and would like to know, how do you change a buttons background colour when it is tapped?
我知道以前可能有人问过这个问题,但是,多年来我一直试图找到答案。我是 Xcode 的新手,想知道如何在点击按钮时更改按钮的背景颜色?
Thanks
谢谢
回答by 4GetFullOf
Below code will change your button's background color to a blackColor.. You can change to whatever color you desire
下面的代码会将您按钮的背景颜色更改为 blackColor.. 您可以更改为您想要的任何颜色
- (IBAction)buttonClicked:(id)sender {
yourButton.backgroundColor = [UIColor blackColor];
}
回答by Greg
You can call setBackgroundColor: in method which fired when button pressed:
您可以调用 setBackgroundColor: 在按下按钮时触发的方法中:
- (IBAction)buttonPressed:(id)sender
{
UIButton *btn = (UIButton*)sender;
[btn setBackgroundColor:[UIColor redColor]];
}