TableView backgroundColor没有改变

时间:2013-02-26 00:30:57

标签: objective-c uitableview background-color

我有一个分组的tableView,我正在尝试将默认背景更改为自定义颜色。我看了一遍,最接近工作的是:

- (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;
}

此代码将背景更改为白色,但我无法将其更改为自定义颜色。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:7)

您正在错误地创建颜色。 RGBA值需要在0.0到1.0的范围内。 UIColor会将1.0以上的任何内容视为1.0。因此,您的颜色将设置为白色,因为所有三个RGB值都将被视为1.0。另请注意,alpha为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 :(得分:3)

您的RGBA值应为0.0 - 1.0。

确保alpha值不应为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;