2

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.

DenVog
  • 4,226
  • 3
  • 43
  • 72

3 Answers3

3

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.

Community
  • 1
  • 1
The Kraken
  • 3,158
  • 5
  • 30
  • 67
  • Thanks for your reply. Isn't UITableView a subclass of UIScrollView which is a subclass of UIView? If so, and UIView conforms to UIAppearance, wouldn't any subclass of UIView implement UIAppearance support, including UITableView? Feel free to correct me if I've lost the plot. – DenVog May 25 '13 at 16:06
  • 1
    Actually, it's not that `UIAppearance` isn't supported by `UITableView`. It just doesn't support the `backgroundColor` property of it. – The Kraken May 25 '13 at 16:07
  • Thank you for clarifying. The link is helpful too. – DenVog May 25 '13 at 16:25
  • No problem. There's also a page containing a list of all methods supported by `UIAppearance`. I'll put it in the answer. – The Kraken May 25 '13 at 16:28
  • 1
    This is not currently true, `setBackgroundColor` does with with `UIAppearance` for `UITableView`. Tested on iOS 9.2. – AnthonyMDev Mar 03 '16 at 23:07
0
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

return YES; }

0

[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

N3al
  • 171
  • 1
  • 6