Set UITableView BackgroundColor Universally with U

2019-07-20 04:57发布

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.

3条回答
Bombasti
2楼-- · 2019-07-20 05:23
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

return YES; }

查看更多
迷人小祖宗
3楼-- · 2019-07-20 05:25

[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

查看更多
倾城 Initia
4楼-- · 2019-07-20 05:32

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.

查看更多
登录 后发表回答