问题描述
我正在尝试普遍更改表格视图的背景颜色。它是UINavigationController和TabBarController应用程序的组合。我已经尝试将以下内容放入AppDelegate applicationDidFinishLaunchingWithOptions
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]];
无变化。
如果我尝试要改变AppDelegate中的一般UIView,这可行:
If I try to change general UIView in AppDelegate, this works:
[[UIView appearance] setBackgroundColor:[UIColor redColor]];
如果我在viewDidLoad中单独攻击每个tableview,这可行:
If I attack each tableview individually in viewDidLoad, this works:
self.tableView.backgroundColor = [UIColor redColor];
我意识到这只是一行代码,但是有很多观点,这只是另一回事不要跟丢。似乎iOS 5 UIAppearance就是为此而制作的。我不清楚它为什么不起作用。谢谢。
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.
推荐答案
UIAppearance
技术上不支持 setBackgroundColor:
,这就是为什么你没有看到所有表视图颜色的变化。您最好的选择是子类 UITableView
。
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.
对于未来的观众,是一个答案的链接,其中包含 UIAppearance
支持的所有方法的列表。
For future viewers, here is a link to an answer containing a list of all methods supported by UIAppearance
.
这篇关于使用UIAppearance通用设置UITableView BackgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!