2013-02-26 68 views
3

我有一個分組tableView,我試圖將默認背景更改爲自定義顏色。我到處都找過,而最接近的工作是這樣的:TableView backgroundColor不變

- (void)viewDidLoad { 
    UIColor *backgroundColor = [UIColor colorWithRed:181 green:293 blue:223 alpha:0]; 
    self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds]; 
    self.tableView.backgroundView.backgroundColor = backgroundColor; 
} 

此代碼更改背景爲白色,但我不能讓它改變到自定義顏色。有人可以幫我嗎?

回答

7

您正在創建顏色不正確。 RGBA值需要在0.0 - 1.0的範圍內。 UIColor會將1.0以上的任何內容視爲1.0。所以你的顏色被設置爲白色,因爲所有三個RGB值都將被視爲1.0。還要注意,0的α意味着完全透明。你想1.0意味着完全可見。

- (void)viewDidLoad { 
    UIColor *backgroundColor = [UIColor colorWithRed:181/255.0 green:293/255.0 blue:223/255.0 alpha:1.0]; 
    self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds]; 
    self.tableView.backgroundView.backgroundColor = backgroundColor; 
} 

請注意,您的綠色值是293,需要更改爲從0東西255

+1

感謝您的幫助,但是這個代碼不工作。背景仍然是白色的。 – 2013-02-26 01:20:32

+0

該alpha應該是1.0,而不是0.0。 – rmaddy 2013-02-26 01:25:00

+1

這樣做!再次感謝。 – 2013-02-26 01:26:30

3

你的RGBA值應爲0.0 - 1.0。

確保阿爾法值不應0.0,看到的顏色效果

UIColor *backgroundColor = [UIColor colorWithRed:0.7 green:1.0 blue:0.85 alpha:1.0]; 
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds]; 
self.tableView.backgroundView.backgroundColor = backgroundColor;