简体   繁体   English

iphone UIButton 背景色

[英]Iphone UIButton background color

I'm creating some UIButton s programmatically in a loop but I'm having some problem with setting the background color of the button.我正在循环中以编程方式创建一些UIButton s,但我在设置按钮的背景颜色时遇到了一些问题。

The color of the button always shows up as white.按钮的颜色始终显示为白色。 But works fine with I'm only using 2 colors in the backgroundcolor.但我只在背景色中使用 2 colors 时效果很好。 Eg: red: 255 green:0 blue:200例如:红色:255 绿色:0 蓝色:200

Here is the code I'm using to add the button.这是我用来添加按钮的代码。

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(80, 20 + (i*75), 200, 75);
    button.layer.cornerRadius = 10;
    button.layer.borderWidth = 1;
    [button setTitle:@"saf" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(moveLocation:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[UIColor colorWithRed:255 green:180 blue:200 alpha:1]];
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [scrollView addSubview:button];

I believe you are building your UIColor wrong.我相信你正在构建你的 UIColor 错误。

Try this:尝试这个:

[button setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(180/255.0) blue:(200/255.0) alpha:1]];
UIColor colorWithRed: green: blue

accepts CGFloats between 0.0 and 1.0接受 0.0 到 1.0 之间的 CGFloats

Here is the api reference.这是 api 参考。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM