更改 UIButton 背景颜色 [英] Changing UIButton background color

查看:58
本文介绍了更改 UIButton 背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建按钮.我想在再次触摸内部时更改按钮背景颜色,它必须在抬起手指后恢复为通常的颜色.....

I am creating buttons programmatically. I want to change button background color while touching up inside again it has to set back to its usual color after lifting up our finger.....

nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:@"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:@selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:nine];

//改变背景颜色

-(void)changeButtonBackGroundColor:(id) sender
{
    [nine setBackgroundColor:[UIColor redColor]];
}

这里创建了 changeBackgroundColor 方法来更改该按钮的颜色.它会改变颜色.

Here changeBackgroundColor method was created to change color of that button . it changes color.

推荐答案

不知道这是否与您的问题有关,但是:这个

Don't know if this relates to your question but: this

[nine setBackgroundColor:[UIColor redColor]];

应该

[sender setBackgroundColor:[UIColor redColor]];

改变这个

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchCancel];

并添加方法:

- (void)resetButtonBackGroundColor: (UIButton*)sender {
    [sender setBackgroundColor:[UIColor cyanColor]];
}

这篇关于更改 UIButton 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆