2011-07-06 36 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