xcode 如何设置或更改 IBAction 上的按钮背景颜色

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

How to set or change button background color on IBAction

xcodebutton

提问by AlextaNET

I try to set the background color of a button, but, isn't work. Some can helpme please?

我尝试设置按钮的背景颜色,但是不起作用。有人可以帮我吗?

- (IBAction)OrBoton:(id)sender {
     [sender setBackgroundColor:(NSColor *)redColor];     
}

But, if I try to setTitle, this works fine. Is unacessible the Backgroundcolor property? I don't recived any error when I Build and Debug, but don't works.

但是,如果我尝试 setTitle,这可以正常工作。无法访问 Backgroundcolor 属性?我在构建和调试时没有收到任何错误,但不起作用。

Thanks!

谢谢!

采纳答案by AlextaNET

Well, finally, I opted for change image button from a colored image, because unfortunately, at the moment, this is impossible for me.

好吧,最后,我选择了从彩色图像更改图像按钮,因为不幸的是,目前这对我来说是不可能的。

回答by iPrabu

Try this

尝试这个

- (IBAction)OrBoton:(id)sender 
{
   UIButton *button = (UIButton *)sender;
   [button setBackgroundColor:[UIColor redColor]];
}

回答by Yogesh Tarsariya

Try This.

尝试这个。

- (IBAction)Click:(UIButton *)sender
{
    if (sender.selected)
    {
        sender.selected = NO;
        sender.backgroundColor=[UIColor whiteColor];
    }
    else
    {
        sender.selected = YES;
        sender.backgroundColor=[UIColor blueColor];
    }
}