Search code examples
iosuitableviewios5uiappearance

Set UITableView BackgroundColor Universally with UIAppearance


I'm trying to universally change the background color for my table views. It is a combination UINavigationController and TabBarController app. I've tried putting the following in AppDelegate applicationDidFinishLaunchingWithOptions

[[[UITableView appearance] backgroundView] setBackgroundColor:[UIColor redColor]];
[[UITableView appearance] setBackgroundColor:[UIColor redColor]];
[[UITableView appearanceWhenContainedIn:[UINavigationController class], nil] setBackgroundColor:[UIColor greenColor]];
[[UITableView appearanceWhenContainedIn:[UITabBarController class], nil] setBackgroundColor:[UIColor greenColor]];

No change.

If I try to change general UIView in AppDelegate, this works:

[[UIView appearance] setBackgroundColor:[UIColor redColor]];

If I attack each tableview individually in viewDidLoad, this works:

self.tableView.backgroundColor = [UIColor redColor];

I realize it's just one line of code, but with a lot of views, it's just another thing to keep track of. It seems like the iOS 5 UIAppearance was made for this. I'm not clear why it isn't working. Thanks.


Solution

  • UIAppearance doesn't technically support setBackgroundColor:, which is why you aren't seeing a change in all table view colors. Your best option will be to subclass UITableView.

    If you need information on how to do that, see this answer.

    For future viewers, here is a link to an answer containing a list of all methods supported by UIAppearance.