Ios 更改从xib加载的UITableViewHeaderFooterView上的背景色表示改为使用contentView.backgroundColor

Ios 更改从xib加载的UITableViewHeaderFooterView上的背景色表示改为使用contentView.backgroundColor,ios,objective-c,uitableview,uiview,Ios,Objective C,Uitableview,Uiview,我很好,几乎一切都正常工作 问题是,现在当我尝试更改背景颜色时(或者如果我在xib中配置了背景颜色),它会不断地将此消息输出到控制台: 不推荐在UITableViewHeaderFooterView上设置背景色。请改用contentView.backgroundColor 这意味着我有两个问题: 如果我不想看到这个警告,我必须去掉xib文件中的背景色(这是不可取的,因为这意味着我的xib不再反映运行时视图的样子) 当我试图通过代码更改背景颜色时,我得到了contentView.backgrou

我很好,几乎一切都正常工作

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

不推荐在UITableViewHeaderFooterView上设置背景色。请改用contentView.backgroundColor

这意味着我有两个问题:

  • 如果我不想看到这个警告,我必须去掉xib文件中的背景色(这是不可取的,因为这意味着我的xib不再反映运行时视图的样子)
  • 当我试图通过代码更改背景颜色时,我得到了
    contentView.backgroundColor
    建议,但当我尝试遵循该建议时,什么也没有发生。(这是因为
    contentView
    nil
注意:这里有一个问题,但主要是关于屏蔽消息,而不是找到解决上述两个问题的替代解决方案


更新:说得很清楚,我想继续使用xib文件作为标题视图,并希望能够调用,以便表能够有效地管理视图。

以下是我找到的解决此问题的最佳方法:

  • UITableViewHeaderFooterView
    的背景色重置为默认值
  • UITableViewHeaderFooterView
    实例的正下方添加一个视图,并将其称为内容视图。(这正是苹果使用
    UITableViewCell
    所做的,我们只是在模仿这种结构。)
  • 现在可以将内容视图的背景色更改为xib文件中所需的任何颜色
  • 将任何其他视图
    放在
    内容视图内
  • 在扩展方法中重新定义
    contentView
    属性,并将
    IBOutlet
    添加到其定义中。(请参见下面的代码。)
  • 将属性与您创建的内容视图相关联,就像与任何IBOutlet相关联一样
  • 现在,您可以使用代码中的
    contentView.backgroundColor
    更改背景色,正如错误消息告诉您的那样
  • .h文件:

    @interface ABCHeaderView : UITableViewHeaderFooterView
    @end
    
    .m文件:

    @interface ABCHeaderView ()
    
    @property (nonatomic, readwrite, retain) IBOutlet UIView *contentView;
    
    @end
    
    @implementation ABCHeaderView
    
    @synthesize contentView;
    
    @end
    
    此层次结构与以下内容一致:

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


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

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


    使用代码创建一个自定义标题并放置它如何

    - (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;
    }
    

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

  • 在Xcode中找到header.xib
  • 右键单击-打开为->源代码
  • 查找颜色键:Cmd-F-“backgr”
  • 移除它。就我而言,这是一句话:
  • 现在您可以更改背景,如:

    override func awakeFromNib() {
    
        backgroundView = UIView()
        backgroundView?.backgroundColor = UIColor.whiteColor()
    }
    
  • 这对我很有用:

    -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
        {
    
            UITableViewHeaderFooterView *headerView=(UITableViewHeaderFooterView *)view;
            [headerView.backgroundView setBackgroundColor:[[UIColor blackColor]colorWithAlphaComponent:0.4]];
        }
    
    已在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文件实际执行的操作)。

    请尝试下面的扩展代码

    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
    }
    

    不幸的是,这不起作用,因为在这个方法中
    contentView
    仍然是
    nil
    。这很可能是因为视图是由xib文件创建的。它是UITableViewHeaderFooterView的一个子类,它的创建方式与.Sounds radar一样。我希望通过xib创建视图,以便在Interface Builder中进行设计。我更新了问题以反映这一事实。我使用以下行作为outlet声明,而不是@property(非原子、只读、保留)IBOutlet UIView*contentView;否则会有一个警告。我在xcode控制台上也有同样的警告,我知道你的答案,谢谢你详细的回答+1。谢谢你。我想我必须设置自定义UIView的框架,但是不,这很简单:)对我来说很好。谢谢。
    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
    }