如何设置iphone中特定按钮的背景色?

如何设置iphone中特定按钮的背景色?,iphone,objective-c,uibutton,Iphone,Objective C,Uibutton,如何设置iPhone中特定按钮的背景色 我用过: btnName1 = [ColorfulButton buttonWithType:UIButtonTypeRoundedRect]; btnName1.frame=CGRectMake(45,146,220,40); [btnName1 setTitle:@"Continue" forState:UIControlStateNormal]; [btnName1 setTitleColor:[UIColor blackColor] forSta

如何设置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中没有显示颜色)

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

请注意,这仅设置背景颜色

如果要更改按钮的外观,请执行以下操作:

[myButton setBackgroundImage:[UIImage imageNamed:@"MyImage.png"] forState:UIControlStateNormal];
您可以将状态更改为您喜欢的任何状态


编辑:如果要从interface builder添加按钮,请确保将按钮
类型更改为
自定义
,并更改
图像

当代码在viewdidload()中写入时,我有一个解决方案,它可以正常工作,而我在uiview中使用时,它为什么不工作。

我就是这样完成的

  • 在.h文件中,将您的按钮声明为IBOutlet

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

    [yourButton setBackgroundColor:[UIColor blueColor]];
    

  • 为了获得我需要的颜色,我使用链接。

    这是这个问题的重复:谢谢!这很有帮助。
    [yourButton setBackgroundColor:[UIColor blueColor]];
    
    [yourButton setBackgroundColor:[UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0]];