【问题标题】:UITableViewHeaderFooterView: Unable to change background colorUITableViewHeaderFooterView:无法更改背景颜色
【发布时间】:2013-03-14 08:23:18
【问题描述】:

我正在尝试更改 UITableViewHeaderFooterView 的背景颜色。尽管视图正在出现,但背景颜色仍然是默认颜色。我从 xcode 收到一条日志:

在 UITableViewHeaderFooterView 上设置背景颜色已经 已弃用。请改用 contentView.backgroundColor。

但是,以下选项均无效:

myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor];

我也尝试在 xib 文件中更改视图的背景颜色。

有什么建议吗?谢谢。

【问题讨论】:

  • 在 iOS 13 中,使用 contentView.backgroundColor 为我工作。我认为 Apple 更新了这个

标签: iphone ios xcode uitableview


【解决方案1】:

iOS 8、9、10、11...

设置任何颜色(带有任何 alpha)的唯一方法是使用backgroundView

斯威夫特

self.backgroundView = UIView(frame: self.bounds)
self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)

Obj-C

self.backgroundView = ({
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5];
    view;
    });

评论回复

  • 这些其他选项都不能可靠地工作(尽管有下面的 cmets)

    // self.contentView.backgroundColor = [UIColor clearColor];
    // self.backgroundColor = [UIColor clearColor];
    // self.tintColor = [UIColor clearColor];
    
  • backgroundView 会自动调整大小。 (无需添加约束)

  • 使用 UIColor(white: 0.5, alpha: 0.5)backgroundView.alpha = 0.5 控制 alpha。
    (当然,任何颜色都可以)

  • 使用XIB时,将根视图设为UITableViewHeaderFooterView,并以编程方式关联backgroundView

    注册:

    tableView.register(UINib(nibName: "View", bundle: nil),
                       forHeaderFooterViewReuseIdentifier: "header")
    

    加载方式:

    override func tableView(_ tableView: UITableView,
                            viewForHeaderInSection section: Int) -> UIView? {
        if let header =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") {
            let backgroundView = UIView(frame: header.bounds)
            backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
            header.backgroundView = backgroundView
            return header
        }
        return nil
    }
    

↻ replay animation

► 在GitHub 上查找此解决方案,在Swift Recipes 上查找更多详细信息。

【讨论】:

  • contentView 不存在,这就是原因。 tintcolor 是为视图而不是其背景着色。和 backgroundcolor 已被弃用。因此,你的方式就是让它发挥作用的方式。顺便说一句,代码语法不错
  • @JoãoNunes,使用 C# / Xamarin 的语法更加简洁:view.BackgroundView = new UIView(view.Bounds) { BackgroundColor = UIColor.Clear };
  • 在 Xcode 6.1.1 中,您会收到运行时控制台警告:Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.
  • 使用 Xcode6.2 beta5,我仍然收到同样的警告。我的 UITableViewHeaderFooterView 的创建方法是基于加载 xib 的,类似于 Apple 示例代码 TVAnimationsGestures。但是,使用 Xcode6.2b5 的 TVAnimationGestures 不会生成任何警告消息。 Apple 代码根本没有“背景”字符串。当然,在 TVAnimationsGestures 中 uitableVuewHeaderFooterview.contentView 是 nil。我不明白为什么会这样。
  • 对于仍然收到警告的人:确保 UITableViewHeaderFooterView 没有在 IB 中设置 backgroundColor。
【解决方案2】:

您应该使用myTableViewHeaderFooterView.tintColor,或者将自定义背景视图分配给myTableViewHeaderFooterView.backgroundView

【讨论】:

  • myTableViewHeaderFooterView.tintColor 完美运行。感谢您的回答!
  • 不知道为什么,但对我来说 tintColor 不适用于 iOS7。我只能通过分配自定义视图来更改颜色:myTableViewHeaderFooterView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
  • TintColor 不是背景色!!
  • 如果您的view background 设置为Default 颜色而不是System Background Default 颜色,则覆盖.tintColor 有效。但不应该使用它,因为它应该是淡色,而不是背景色。
【解决方案3】:

在 iOS 7 中 contentView.backgroundColor 为我工作,tintColor 没有。

   headerView.contentView.backgroundColor = [UIColor blackColor];

虽然clearColor 对我不起作用,但我找到的解决方案是将backgroundView 属性设置为透明图像。也许它会帮助某人:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

headerView.backgroundView = [[UIImageView alloc] initWithImage:blank];

【讨论】:

  • 我认为这是比@SwiftArchitect 的答案更好的方法(尽管它有效)。您不必添加新的 UIView 的事实使它变得更好。非常适合我。
【解决方案4】:

确保为UITableViewHeaderFooterView 设置contentView 的背景颜色:

self.contentView.backgroundColor = [UIColor whiteColor];

然后就可以了。

【讨论】:

  • 仅限 iOS >=10!。对于 iOS
【解决方案5】:

对我来说,我尝试了上述所有方法,但仍然收到警告 “在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改用 contentView.backgroundColor。”然后我尝试了这个: 在 xib 文件中,选择标题视图的背景颜色以清除颜色而不是默认颜色,一旦我将其更改为默认值,警告就消失了。

【讨论】:

    【解决方案6】:

    对于纯色背景色,设置contentView.backgroundColor 就足够了:

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let headerView = view as? UITableViewHeaderFooterView {
            headerView.contentView.backgroundColor = .red // Works!
        }
    }
    

    对于具有透明度的颜色,包括.clear 颜色,这不再有效:

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let headerView = view as? UITableViewHeaderFooterView {
            headerView.contentView.backgroundColor = .clear // Does not work ?
        }
    }
    

    对于完全透明的部分标题,将backgroundView 属性设置为空视图:

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let headerView = view as? UITableViewHeaderFooterView {
            headerView.backgroundView = UIView() // Works!
        }
    }
    

    但是,请注意可能的副作用。除非表格视图设置为“分组”,否则在向下滚动时,部分标题将在顶部对齐。如果部分标题是透明的,则单元格内容将被看穿,这可能看起来不太好。

    这里,节标题有透明背景:

    为防止这种情况,最好将节标题的背景设置为与表格视图或视图控制器的背景相匹配的纯色(或渐变)。

    在这里,节标题具有完全不透明的渐变背景:

    【讨论】:

    • 天才!为 backgroundView 定义一个空的 UIView 完全解决了我的问题!谢谢!
    【解决方案7】:

    为了颜色清晰,我使用

    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [UIColor clearColor];
    

    我觉得很好。

    【讨论】:

    • 设置backgroundView = UIView() 然后backgroundColor = .clear 是唯一可行的解​​决方案。谢谢。
    【解决方案8】:

    在界面构建器中拉起您的 .xib,在顶部元素中,在属性检查器中将背景颜色设置为默认值。然后转到 Content View 并在那里设置背景颜色(参考https://github.com/jiecao-fm/SwiftTheme/issues/24)。

    【讨论】:

      【解决方案9】:

      iOS9 headerView.backgroundView.backgroundColor 为我工作:

      - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          TableViewHeader *headerView = (TableViewHeader *)[super tableView:tableView viewForHeaderInSection:section];
          headerView.backgroundView.backgroundColor = [UIColor redColor];
      }
      

      iOS8 上,我使用 headerView.contentView.backgroundColor 没有问题,但现在使用 iOS 9,我遇到了一个奇怪的问题,即背景颜色没有填满单元格的整个空间。所以我只尝试了headerView.backgroundColor,我从 OP 得到了同样的错误。

      在 UITableViewHeaderFooterView 上设置背景颜色已经 已弃用。请改用 contentView.backgroundColor。

      所以现在一切正常,使用headerView.backgroundView.backgroundColor没有警告

      【讨论】:

        【解决方案10】:

        如果您使用xib 文件创建了UITableViewHeaderFooterView 的自定义子类,那么您应该覆盖setBackgroundColor。保持空白。

        -(void)setBackgroundColor:(UIColor *)backgroundColor {
        
        }
        

        这将解决您的问题。

        【讨论】:

        • 这对我来说是正确的答案。虽然我没有在我的 HeaderFooterView 中设置任何背景颜色,但警告消息不断出现在我的控制台日志中。太烦人了!
        【解决方案11】:

        如果您是customising a section header cell with Storyboard/Nib,请确保“表节标题”视图的背景颜色为默认

        如果您将UITableViewHeaderFooterView 子类化并使用nib,那么您要做的就是为内容视图创建一个IBOutlet,并将其命名为例如。 containerView。不要与 contentView 混淆,后者是该容器视图的父级。

        使用该设置,您可以改为更改 containerView 的背景颜色。

        【讨论】:

          【解决方案12】:

          我尝试使用 appearanceWhenContainedIn 链,它对我有用。

          [[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], [UITableView class], nil] 
                   setBackgroundColor: [UIColor.grayColor]];
          

          【讨论】:

            【解决方案13】:

            可能是因为 backgroundView 不存在

            override func draw(_ rect: CGRect){
                // Drawing code
                let view = UIView()
                view.frame = rect
                self.backgroundView = view
                self.backgroundView?.backgroundColor = UIColor.yourColorHere
            }
            

            这对我有用。

            【讨论】:

              【解决方案14】:

              iOS 12、Swift 5。如果您愿意(或尝试!)使用外观代理,以下解决方案有效:

              1. 继承 UITableViewHeaderFooterView 并公开您自己的外观代理设置。
              final class MySectionHeaderView: UITableViewHeaderFooterView {
                  required init?(coder aDecoder: NSCoder) {
                      super.init(coder: aDecoder)
                  }
              
                  override init(reuseIdentifier: String?) {
                      super.init(reuseIdentifier: reuseIdentifier)
                  }
              
                  @objc dynamic var forceBackgroundColor: UIColor? {
                      get { return self.contentView.backgroundColor }
                      set(color) {
                          self.contentView.backgroundColor = color
                          // if your color is not opaque, adjust backgroundView as well
                          self.backgroundView?.backgroundColor = .clear
                      }
                  }
              }
              
              1. 以所需的粒度设置外观代理。
              MySectionHeaderView.appearance().forceBackgroundColor = .red
              

              MySectionHeaderView.appearance(whenContainedInInstancesOf: [MyOtherClass.self]).forceBackgroundColor = .red
              

              【讨论】:

                【解决方案15】:

                忘记困难。

                使用以下代码添加到您的项目UITableViewHeaderFooterView+BGUpdate.swift

                extension UITableViewHeaderFooterView {
                
                    open override var backgroundColor: UIColor? {
                        get {
                            return self.backgroundColor
                        }
                        set {
                            let bgView = UIView()
                            bgView.backgroundColor = newValue
                            backgroundView = bgView
                        }
                    }
                }
                

                用法和你之前想象的一样简单:

                headerView.backgroundColor = .red
                

                使用示例

                1) 在委托的tableView:viewForHeaderInSection: 中:

                func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                    let headerView = tv.dequeueReusableHeaderFooterView(withIdentifier: "MyHeaderView")
                    headerView.backgroundColor = .red // <- here
                    return headerView
                }
                

                2) 在您的自定义标题视图类中:

                class MyHeaderView: UITableViewHeaderFooterView {
                
                    override func awakeFromNib() {
                        super.awakeFromNib()
                        backgroundColor = .red // <- here
                    }
                }
                

                【讨论】:

                • 好吧...你的方式更好。 :) 谢谢
                • 此解决方案会在 iOS 12 上导致递归循环。
                【解决方案16】:

                将此代码放入您的 UITableViewHeaderFooterView 子类的初始化中:

                let bgView = UIView()
                bgView.backgroundColor = UIColor.clear
                backgroundView = bgView
                backgroundColor = UIColor.clear
                contentView.backgroundColor = UIColor.clear
                

                【讨论】:

                • 虽然这可能会回答问题,但它已被标记为待审核。没有解释的答案通常被认为是低质量的。请在答案中提供一些评论,说明为什么这是正确答案。
                • 我明白了。感谢您的反馈。
                【解决方案17】:

                用清晰的颜色设置 BackgroundView 对于可见的标题效果很好。 如果表格滚动到底部显示标题,则此解决方案失败。

                P.S.我的表格只包含没有任何单元格的标题。

                【讨论】:

                  【解决方案18】:

                  斯威夫特:

                  func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
                      var headerView: TableViewHeader = super.tableView(tableView, viewForHeaderInSection: section) as! TableViewHeader
                      headerView.backgroundView.backgroundColor = UIColor.redColor()
                  }
                  

                  【讨论】:

                    【解决方案19】:

                    创建 UIView 并设置背景颜色,然后将其设置为 self.backgroundView。

                    - (void)setupBackgroundColor:(UIColor *) color {
                        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
                        bgView.backgroundColor = color;
                        self.backgroundView = bgView;
                    }
                    

                    【讨论】:

                      【解决方案20】:

                      我觉得有必要分享我的经验。 我的这段代码在 iOS 10 和 iOS 11 headerView?.contentView.backgroundColor = .lightGray 上运行良好

                      然后我突然决定为 iOS 9 部署应用程序,因为有一些设备(iPad mini 一些老一代不会更新到任何超过 9 的操作系统) - 唯一适用于所有 iOS 9 的解决方案, 10 和 11 是为标题定义一个基本视图,然后包含所有其他标题子视图,从情节提要连接它并设置该基本视图的backgroundColor

                      在连接插座时要注意不要调用它:backgroundView,因为在某些superclass 中已经有一个具有该名称的属性。我打电话给我的containingView

                      此外,在连接插座控件时,单击 Document Outline 中的视图以确保它没有连接到 file owner

                      【讨论】:

                        猜你喜欢
                        • 2014-07-06
                        • 2021-06-07
                        • 2016-10-04
                        • 2014-09-25
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多