2011-07-06 9 views
5

私はループでプログラムでUIButtonを作成していますが、ボタンの背景色を設定する際に問題があります。Iphone UIButton背景色

ボタンの色は常に白く表示されます。 しかし、私は背景色で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〜255ではなく0〜1です。 – Steve

+0

ありがとう!私はいつもパーセンテージ値の代わりにrgbの数値であったが、 – Tim