2011-07-06 135 views
5

我创建了一些UIButton s以循环方式编程,但我在设置按钮的背景颜色时遇到了一些问题。Iphone UIButton背景颜色

按钮的颜色总是显示为白色。 但工作正常,我只在backgroundcolor中使用2种颜色。 例如:红色:255绿色:0蓝色:200

这里是我用来添加按钮的代码。

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]; 

回答

20

我相信你正在构建你的UIColor错误。

尝试这种情况:

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

+1绝对如此。同意。组件是0-1而不是0-255。 – Steve

+0

谢谢!我总是尽管它是rgb的数字值而不是百分比值。 – Tim