【问题标题】:Change background of a grouped UITableView更改分组 UITableView 的背景
【发布时间】:2012-06-15 08:20:47
【问题描述】:

我在尝试使用组更改 UITableView 的背景时遇到了一些问题。

_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBg.png"]];

这通常适用于所有其他 UITableView,但不适用于带有组的 UITableView,我还有什么需要做的吗? 在 IB 中,我将背景颜色设置为清晰的颜色,但这并没有多大作用。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您还需要禁用_tableView的背景视图:

    [_tableView setBackgroundView:nil];
     _tableView.backgroundColor = [UIColor redColor];
    

    无需向 backgroundView 添加新视图。这在iOS6 中为我工作。

    【讨论】:

    • 大大节省了时间。
    • 天才。谁知道分组的部分标题很清楚!无论我们现在在 2017 年使用什么 iOS,您都不需要清除 tableView 的背景。我只是将它直接设置为白色,这就是她写的全部内容。感谢您帮我解决这个问题!
    • @MikeCritchley 这个答案是在 2013 年给出的,因为当时没有实施。
    【解决方案2】:

    为什么不设置tableView.backgroundView?您可以使用指定的图像分配图像视图并将其传递给背景视图,而不是设置背景颜色。

    【讨论】:

    • 那里的tableView.backgroundColor不是用来改变背景颜色的吗?使用 tableView.backgroundView 似乎是一个漫长的解决方法
    • 其实你的回答帮了我,原来你为什么要分组一个UITableView,设置了backgroundView,使用self.tableView.backgroundView = nil;你删除它
    【解决方案3】:

    我通常对分组 UITableViews 做的是将背景颜色设置为清除,并将该模式​​图像设置为父视图。

    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableBG.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    

    【讨论】:

    • 我也尝试过,但没有成功:self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tableViewBg.png"]]; _tableView.backgroundColor = [UIColor clearColor];
    • 如果我没记错的话 UITableView 有一个滚动视图背景..试试这个..添加到我的答案中。
    • 嗯,这也没什么用,我不明白为什么它在这里不起作用,在任何其他表中我使用相同的代码都可以正常工作
    • +1 这对我来说效果很好,尤其是当我将 backgroundview 设置为 nil 时。
    【解决方案4】:
    tableView.backgroundView = nil;
    tableView.backgroundColor = [UIColor colorWithPatternImage: xxx];
    // tableView.backgroundColor = [UIColor redColor];  // is ok
    

    如果你将 backgroundColor 设置为这样,当你滚动 tableView 时,backgroundColor 视图也会滚动。所以,你可以: tableView.backgroundView = nil; self.view.backgroundColor = ...

    【讨论】:

      【解决方案5】:

      选择的答案有效,但它清除了 tableHeaderView 和表格部分标题视图的背景。如果您只想让表格标题具有某种颜色,请说白色,而部分标题仍然是默认灰色,而不是执行以下操作 -

      tableView.tableHeaderView.inputView.backgroundColor = [UIColor whiteColor];
      

      【讨论】:

        【解决方案6】:

        只是想添加到 Nirav 的答案 - 也可以使用 iOS 5 外观代理来完成。

        [[UITableView appearance] setBackgroundView:nil];
        [[UITableView appearance] setBackgroundColor:[UIColor lightGreyColor]];
        

        优点是它适用于全球,因此您可以将所有 UI 自定义设置在一个位置。但是,它将适用于所有 tableViews(不仅仅是分组样式)。

        【讨论】:

          【解决方案7】:

          对于iOS 9+,改变UITableView的背景色就足够了。

          Objective-C

          tableView.backgroundColor = [UIColor redColor];
          

          斯威夫特

          tableView.backgroundColor = .red
          

          【讨论】:

            【解决方案8】:

            Swift 4.2

            移除背景视图和颜色

            tableView.backgroundView = nil
            tableView.backgroundColor = .clear
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-15
              • 2013-01-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多