【问题标题】:Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead更改从 xib 加载的 UITableViewHeaderFooterView 的背景颜色说要改用 contentView.backgroundColor
【发布时间】:2014-09-27 12:28:15
【问题描述】:

我是creating a UITableViewHeaderFooterView from a xib file,几乎一切正常。

问题是,现在当我尝试更改背景颜色时(或者如果我在 xib 中配置了背景颜色),它会不断向控制台输出此消息:

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

这意味着我有两个问题:

  • 如果我不想看到那个警告,我必须去掉 xib 文件中的背景颜色。(这是不可取的,因为这意味着我的 xib 不再反映视图在运行时的样子)。
  • 当我尝试通过代码更改背景颜色时,我收到了contentView.backgroundColor 建议,但是当我尝试遵循该建议时,没有任何反应。 (这是因为contentViewnil。)

注意:这里有一个similar question,但主要是关于静音消息,而不是找到解决上述两个问题的替代解决方案。

更新: 明确地说,我想继续使用 xib 文件作为标题视图,并希望能够调用 dequeueReusableHeaderFooterViewWithIdentifier: 以便表格可以有效地管理意见。

【问题讨论】:

    标签: ios objective-c uitableview uiview


    【解决方案1】:

    这是我发现解决此问题的最佳方法:

    1. UITableViewHeaderFooterView 的背景颜色重置为默认
    2. UITableViewHeaderFooterView 的实例下方直接添加一个视图,并将其命名为内容视图。 (这正是 Apple 对 UITableViewCell 所做的事情,我们只是在模仿这种结构。)
    3. 您现在可以在 xib 文件中将内容视图的背景颜色更改为您想要的任何颜色。
    4. 放置任何其他视图insideContent View
    5. 在扩展方法中重新定义contentView属性,并将IBOutlet添加到其定义中。 (见下面的代码。)
    6. 将属性与您创建的内容视图相关联,就像与任何 IBOutlet 一样。
    7. 您现在可以在代码中使用 contentView.backgroundColor 更改背景颜色,就像错误消息告诉您的那样。

    .h 文件:

    @interface ABCHeaderView : UITableViewHeaderFooterView
    @end
    

    .m 文件:

    @interface ABCHeaderView ()
    
    @property (nonatomic, readwrite, retain) IBOutlet UIView *contentView;
    
    @end
    
    @implementation ABCHeaderView
    
    @synthesize contentView;
    
    @end
    

    这个层次结构与Apple's documentation一致:

    如果您有自定义内容要显示,请为您的内容创建子视图并将它们添加到 contentView 属性中的视图。

    【讨论】:

    • 我在出口声明中使用了以下行而不是 @property (nonatomic, readonly, retain) IBOutlet UIView *contentView;否则会有警告。
    【解决方案2】:

    要消除警告,您不能设置背景颜色。问题是当您以这种方式创建 TableHeader 时,IB 没有为此提供特殊对象。 删除源代码中的背景颜色将很好地解决所描述的问题,只需点击几下。

    1. 在 Xcode 中找到头文件 .xib
    2. 右键-打开方式->源码
    3. 查找颜色键:Cmd-F - "backgr"
    4. 删除它。就我而言,它是这条线: <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
    5. 现在你可以像这样改变背景:

      override func awakeFromNib() {
      
          backgroundView = UIView()
          backgroundView?.backgroundColor = UIColor.whiteColor()
      }
      

    【讨论】:

    • 我在 xcode 控制台上遇到了同样的警告,我知道你的回答,感谢你的详细回答 +1。
    • 谢谢你。我以为我必须设置自定义 UIView 的框架,但不,这很明显有效:)
    【解决方案3】:

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

    如果您在单独的 xib 文件中设计标题并在其中设置主 UIView 的背景,则会发生这种情况。

    为了解决这个问题,在 InterfaceBuilder 中将 UIView 背景颜色恢复为默认颜色,并在 viewForHeaderInSection:section 方法中设置背景颜色。

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderXibFileName") as! CustomHeader
        header.contentView.backgroundColor = UIColor.blueColor
    }
    

    如您所见,我们使用 header.contentView.backgroundColor 而不是 header.backgroundColor(这是 xib 文件实际执行的操作)。

    【讨论】:

      【解决方案4】:

      这对我有用:

      -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
          {
      
              UITableViewHeaderFooterView *headerView=(UITableViewHeaderFooterView *)view;
              [headerView.backgroundView setBackgroundColor:[[UIColor blackColor]colorWithAlphaComponent:0.4]];
          }
      

      【讨论】:

        【解决方案5】:

        试试下面的扩展代码,

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

        并像下面这样分配背景颜色,

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
           guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView") as? CustomHeaderView else {
              return UITableViewHeaderFooterView()
           }
        
           headerView.backgroundColor = .orange
        
           return headerView
        }
        

        【讨论】:

          【解决方案6】:

          在 willAppear 委托回调中设置 contentView.backgroundColor:

          - (void)tableView:(UITableView *)tableView
          willDisplayHeaderView:(UIView *)view
                 forSection:(NSInteger)section
          

          - (void)tableView:(UITableView *)tableView
          willDisplayFooterView:(UIView *)view
                 forSection:(NSInteger)section
          

          【讨论】:

          • 不幸的是,这不起作用,因为在此方法中contentView 仍然是nil。这很可能是因为视图是由 xib 文件创建的。
          • 它是 UITableViewHeaderFooterView 的子类,其创建方式与 the answer I linked to 类似。
          • 听起来雷达对我来说很有价值。
          【解决方案7】:

          如何使用代码创建自定义 Header 并放置它?

          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSinteger)section {
          
                UIView * myHeader = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
                [myHeader setBackgroundColor:[UIColor grayColor]];
          
                UILabel * TitleLabel = [[UILabel alloc] initWithFrame: CGRectMake(10, 5, 320, 20)];
                TitleLabel.text = @"My Title";
          
                [myHeader addSubview:TitleLabel];
          
          
                return myHeader;
          }
          

          【讨论】:

          • 我希望通过 xib 创建视图,以便可以在 Interface Builder 中设计它。我更新了问题以反映这一事实。
          猜你喜欢
          • 2013-03-14
          • 2021-06-07
          • 2014-07-06
          • 1970-01-01
          • 1970-01-01
          • 2017-12-16
          • 2013-09-18
          • 2013-08-08
          相关资源
          最近更新 更多