【问题标题】:Iphone UIButton background colorIphone UIButton 背景颜色
【发布时间】:2011-09-29 07:05:41
【问题描述】:

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

按钮的颜色始终显示为白色。 但是我在背景色中只使用了 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];

【问题讨论】:

    标签: iphone objective-c uibutton background-color


    【解决方案1】:
    UIColor colorWithRed: green: blue
    

    接受 0.0 到 1.0 之间的 CGFloats

    这里是 api 参考。

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

    【讨论】:

      【解决方案2】:

      我相信你的 UIColor 是错误的。

      试试这个:

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

      【讨论】:

      • 谢谢!我一直认为它是 rgb 的数字值而不是百分比值。