我如何设置iPhone中特定按钮的背景颜色?

问题描述:

如何设置iPhone中特定按钮的背景颜色?我如何设置iPhone中特定按钮的背景颜色?

我用:

btnName1 = [ColorfulButton buttonWithType:UIButtonTypeRoundedRect]; 
btnName1.frame=CGRectMake(45,146,220,40); 
[btnName1 setTitle:@"Continue" forState:UIControlStateNormal]; 
[btnName1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[btnName1 setImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal]; 
UIImage *img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"greenish" ofType:@"png"]]; 
UIImage *strechableImage = [img stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
[btnName1 setBackgroundImage:strechableImage forState:UIControlStateNormal]; 
[btnName1 setNeedsDisplay]; 
[InfoView addSubview:btnName1]; 

它的工作在iPhone模拟器上正确,但不是在iPod(彩色iPod中未显示)。

+1

这是这个问题的重复:http://*.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – 2010-04-21 17:37:51

myButton.backgroundColor = [UIColor redColor]; 

或者任何你想要的颜色。该方法从UIView继承。

请注意,这只是设置背景色...

如果要更改按钮的外观:

[myButton setBackgroundImage:[UIImage imageNamed:@"MyImage.png"] forState:UIControlStateNormal]; 

你可以改变你喜欢的任何状态的状态。

编辑:如果您从界面生成器添加按钮,请务必将按钮Type更改为Custom,并更改image

+0

我用这个耳机,但它不能在iPod上工作。 – 2010-04-21 12:46:21

+0

@suchita它可以在模拟器中正常工作吗? – Warrior 2010-04-21 13:22:44

+0

是的,你能否详细说明这个问题?我认为这应该是这样做的... – 2010-04-21 13:28:02

我有解决方案,当代码写入viewdidload()它是适当的工作eariler我用于uiview这是因为它不工作。

这就是我如何完成它。

  1. 在.h文件中将yourButton声明为IBOutlet。

    @property (weak, nonatomic) IBOutlet UIButton *yourButton; 
    
  2. 在.m文件要更改按钮的颜色请使用

    [yourButton setBackgroundColor:[UIColor blueColor]]; 
    

[yourButton setBackgroundColor:[UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0]]; 

为了得到我需要我的颜色使用this链接。