【问题标题】:iOS 8 UITableView background color appearanceiOS 8 UITableView 背景颜色外观
【发布时间】:2014-08-27 15:09:52
【问题描述】:

Xcode 6 beta 6,尝试在外观代理中更改所有 UITableView 的背景颜色:

[[UITableView appearance] setBackgroundColor:[UIColor redColor]]

但是好像不行。

重现步骤:

1 创建单视图项目

2 在 storyboard

中将 UITableView 添加到 ViewController

3 在 IB 中设置代理以查看控制器并更改背景:

4 添加动态单元格并配置数据源:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1Identifier" forIndexPath:indexPath];
  return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 60.f;
}

5 应用内委托:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  [[UITableView appearance] setBackgroundColor:[UIColor redColor]];

  return YES;
}

6 运行应用程序并观察不正确的颜色:

有什么建议可以解决吗? 为每个表格设置背景颜色看起来不是很好的解决方案。

【问题讨论】:

  • 它只发生在 iOS 8.0.x 上,这是错误的,在更低或更高版本的 8.0.x 中没有产生,我认为大多数用户升级到 8.1 所以完全不用担心:)
  • 根据this 的问题,这似乎是一个错误。 rdar://18406065
  • 我在物理设备和模拟器上的 iOS 8.1.X 和 8.2.X 中仍然遇到 UITableView 外观背景颜色问题。但是它在上面工作正常。我必须以编程方式保留[tableview setBackgroundColor: ]
  • popei,问题是如何在外观代理中自动完成

标签: ios objective-c iphone uitableview ios8


【解决方案1】:

尝试在 InterfaceBuilder 中将 BackgroundColor“重置”为“默认”(即使它已经默认,您也会看到一些颜色变化)

这不适用于分组样式表视图

更新:

这对我有用

[[UIScrollView appearance] setBackgroundColor:[UIColor redColor]];

【讨论】:

  • 即使使用默认颜色,它现在也不起作用。
  • 但问题在于分组样式表
  • 这对我也不起作用。出于某种原因,iOS 8 为我们“破坏”了在表格视图上设置背景颜色的能力。还有其他想法吗?
  • 设置 UIScrollView 的外观允许我更改 tableview 背景颜色 - 但是,我没有设法创建一个具有透明背景的 tableview(在 UIScrollView 外观上设置 SetOpaque 似乎不起作用)
【解决方案2】:

TableViewCell 的清除颜色在 XCode 6 中的平板电脑中不起作用。以下解决方法可以解决我的问题。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

     cell.backgroundColor = [UIColor redColor];
}

【讨论】:

    【解决方案3】:

    似乎对于 iOS 8,至少在我们的情况下,当我们使用该视图中的表格设置视图的背景颜色时,我们必须明确将表格和单元格背景颜色设置为清除为了从视图中显示所需的颜色。我想,在以前的 iOS 版本中,单元格默认为透明,但现在似乎不再如此了。

    【讨论】:

      【解决方案4】:

      将单元格背景颜色设置为透明 tableView:willDisplayCell:forRowAtIndexPath:

      【讨论】:

        【解决方案5】:

        [UITableVIew backgroundColor] 没有用 UI_APPEARANCE_SELECTOR 标记。如果选择器标记为UI_appeArance_Selector,则外观代理只能工作。

        【讨论】:

        • 这应该不是问题,因为 UITableView 继承自 UIView,它的属性 backgroundColor 用 UI_APPEARANCE_SELECTOR 标记。
        【解决方案6】:

        外观代理似乎在 iOS 8.0.x 中出现了一个看似未报告的错误;它与涉及表格单元格默认背景颜色的 iOS 7 更改无关。

        相对的好消息是,这似乎只是 iOS 8.0 和 8.0.2 之间的问题,因为:

        1. 我的 iPhone 4S 7.1.2 没有这个问题,
        2. 我同事的运行 8.1 的 iPhone 6 没有这个问题。

        在大多数用户升级之前,您必须明确地在对象本身上设置背景,让我们回到出现前的时代:(

        【讨论】:

        • 有趣...我在所有 8.1 模拟器中都看到了它。
        猜你喜欢
        • 2015-11-01
        • 1970-01-01
        • 2011-10-07
        • 2023-03-03
        • 1970-01-01
        • 2014-09-04
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多