DenVog
DenVog

Reputation: 4286

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.

Upvotes: 2

Views: 4298

Answers (3)

N3al
N3al

Reputation: 311

[UITableView backgroundColor] selector is not marked with UI_APPEARANCE_SELECTOR. According to Apple documentation

"To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR."

UITableView backgroundColor is not supposed to work with appearance proxy

Upvotes: 0

user1766237
user1766237

Reputation: 9

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[[UITableView appearance] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"xxxx.png"]]];

return YES; }

Upvotes: 0

The Kraken
The Kraken

Reputation: 3158

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.

Upvotes: 3

Related Questions