使用UIAppearance通用设置UITableView BackgroundColor
我正试图普遍改变我的表格视图的背景颜色。 它是UINavigationController和TabBarController应用程序的组合。 我已经尝试在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]];
不用找了。
如果我尝试在AppDelegate中更改常规UIView,则可以:
[[UIView appearance] setBackgroundColor:[UIColor redColor]];
如果我在viewDidLoad中单独攻击每个tableview,这可以工作:
self.tableView.backgroundColor = [UIColor redColor];
我意识到它只是一行代码,但是有很多视图,跟踪是另一回事。 似乎iOS 5 UIAppearance就是为此而制作的。 我不清楚它为什么不起作用。 谢谢。
UIAppearance
在技术上不支持setBackgroundColor:
UIAppearance
这就是为什么你没有看到所有表视图颜色的变化。 您最好的选择是UITableView
。
如果您需要有关如何操作的信息,请参阅此答案。
对于未来的观众, 这里是一个答案的链接,其中包含UIAppearance
支持的所有方法的列表。
- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UITableView外观] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@“xxxx.png”]]];
返回YES; }
[UITableView backgroundColor]选择器未标记为UI_APPEARANCE_SELECTOR。 根据Apple文档
“为了支持外观自定义,类必须符合UIAppearanceContainer协议,并且必须使用UI_APPEARANCE_SELECTOR标记相关的访问器方法。”
UITableView backgroundColor不适用于外观代理