1

I have to set the backgroundColor property of a UITableView to a certain color in my universal app. If I write this...

self.tableView.backgroundColor = [UIColor colorWithRed:146.0f green:197.0f blue:240.0f alpha:1.0f];

...it doesn't work on iPhone nor iPad (background results white).

If I use a standard color, instead...

self.tableView.backgroundColor = [UIColor greenColor];

...it works on iPhone but not on iPad.

I tried other solutions suggested on SO (backgroundView to nil/clearColor, etc.), but none of those works on iOS SDK 5. Can you help me?

Tony Mobile
  • 677
  • 8
  • 19
  • Your RGB values should be between 0.0f and 1.0f. See [this similar question](http://stackoverflow.com/questions/5641523/using-uicolor-colorwithred-green-blue-alpha-doest-work-with-uitableview-s). – Ben Challenor Dec 01 '11 at 10:42
  • I am having the same issue. For iPad with iOS 5.0, it looks like the background color of tableview cannot be changed. Please post the answer if you were able to fix the issue. –  Apr 24 '13 at 14:49

3 Answers3

1

You need to divide each color value to 255. Your color will be:

[UIColor colorWithRed:146/255.0 green:197/255.0 blue:240/255.0 alpha:1.0];
Shmidt
  • 16,436
  • 18
  • 88
  • 136
0

Instead of changing the background color, you can set a backgroundView (with the background color that you like) for the tableView.

UIView *bgView = [[UIView alloc] initWithFrame:self.tableView.bounds];
bgView.backgroundColor = [UIColor redColor];
self.tableView.backgroundView = bgView;
-1

Are you using a navigation controller?

Try this:

 self.tableView.backgroundColor = [UIColor clearColor];
[self.tableView setSeparatorColor:[UIColor clearColor]];
self.navigationController.view.backgroundColor = [UIColor greenColor];

Thats what I use with iOS5 and an universal app.

Christian Loncle
  • 1,584
  • 3
  • 20
  • 30
  • OK. I got a minus. And I understand that my answer is just half the answer because you should indeed divide the numbers with 255. But the problem with the iPad is also to set the tableview background to clearColor because it works differently on the iPad compared to the iPhone. That the explanation why [UIColor greenColor] works on the iPhone and not on the iPad. – Christian Loncle Mar 20 '12 at 21:55