[英]how can i set the background color of a particular button in iphone?
How can I set the background color of a particular button in iPhone? 如何在iPhone中设置特定按钮的背景颜色?
I used: 我用了:
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];
It's correctly working on iPhone simulator but not on iPod (color not shown in iPod). 它在iPhone模拟器上正常工作,但在iPod上没有(iPod中没有显示颜色)。
myButton.backgroundColor = [UIColor redColor];
Or whatever colour you want. 或者你想要的任何颜色。 This method is inherited from UIView
. 此方法继承自UIView
。
Note that this only sets the background colour... 请注意,这仅设置背景颜色......
If you want to change the button's look: 如果你想改变按钮的外观:
[myButton setBackgroundImage:[UIImage imageNamed:@"MyImage.png"] forState:UIControlStateNormal];
You can change the state to whichever state you like. 您可以将状态更改为您喜欢的任何状态。
EDIT: If you are adding the button from interface builder, make sure to change the button Type
to Custom
, and change the image
. 编辑:如果要从界面构建器添加按钮,请确保将按钮Type
更改为Custom
,然后更改image
。
This is how I got it done. 这就是我完成它的方式。
In the .h file declare yourButton as an IBOutlet. 在.h文件中,将yourButton声明为IBOutlet。
@property (weak, nonatomic) IBOutlet UIButton *yourButton;
In the .m file where you want to change the colour of the button use either 在.m文件中,您要更改按钮的颜色使用
[yourButton setBackgroundColor:[UIColor blueColor]];
or 要么
[yourButton setBackgroundColor:[UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0]];
To obtain the colours I require I use this link. 为了获得我需要的颜色,我使用此链接。
当我在viewdidload()中编写代码时,我有了解决方案,这是我用于uiview的正确工作的预告片,因为它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.