ios - 使用 UIAppearance 设置 UITableView 的背景颜色

标签 ios uitableview ios5 uiappearance

我正在尝试普遍更改表格 View 的背景颜色。它是 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];

我意识到这只是一行代码,但是有很多 View ,这只是另一件事需要跟踪。 iOS 5 UIAppearance 似乎就是为此而设计的。我不清楚为什么它不起作用。谢谢。

最佳答案

UIAppearance 在技术上不支持 setBackgroundColor:,这就是为什么您没有看到所有表格 View 颜色发生变化的原因。您最好的选择是子类化 UITableView

如果您需要有关如何执行此操作的信息,请参阅 this answer.

对于 future 的观众,here是指向答案的链接,其中包含 UIAppearance 支持的所有方法的列表。

关于ios - 使用 UIAppearance 设置 UITableView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751067/

相关文章:

ios - 静态单元格 - socket 始终为零

objective-c - UITableView 像联系人应用程序编辑模式

iphone - 我应该针对我的下一个iPhone应用程序定位哪个iOS版本(考虑到Facebook集成)

objective-c - 在不将整个文件加载到 ram 的情况下,通过 HTTP POST 发送大文件的正确方法是什么?

c# - MonoTouch.Dialog 崩溃

ios - 如何让 iOS navigationItem.titleView 左对齐?

iphone - 检查 NSURLRequest 目标是否为 "_blank"?

ios - 重新加载 tableview 时发生断言失败?

iPhone4 SecItemCopyMatching 重启后返回 -25300

ios - 连接 Outlet Collection 故障