改变从一个xib加载的UITableViewHeaderFooterView的背景颜色说,使用contentView.backgroundColor

我从一个xib文件创build一个UITableViewHeaderFooterView ,几乎所有的工作都正常。

问题是,现在当我尝试更改背景颜色(或者如果我有一个configuration在xib中),它会不断输出这个消息到控制台:

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

这意味着我有两个问题:

  • 如果我不想看到这个警告,我必须摆脱xib文件中的背景颜色(这是不受欢迎的,因为这意味着我的xib不再反映视图在运行时的样子)。
  • 当我尝试通过代码更改背景颜色时,我得到了contentView.backgroundColorbuild议,但是当我尝试按照该build议时,没有任何反应。 (这是因为contentViewnil

注:这里有一个类似的问题 ,但主要是关于消息的消息,没有find解决上述两个问题的替代解决scheme。

更新:为了清楚起见,我想继续使用一个xib文件作为标题视图,并且希望能够调用dequeueReusableHeaderFooterViewWithIdentifier:这样表就可以高效地pipe理视图。

这是我发现解决这个问题的最好方法:

  1. 重置您的UITableViewHeaderFooterView的背景颜色为默认值
  2. 直接在UITableViewHeaderFooterView的实例下添加一个视图,并将其称为内容视图 。 (这正是苹果用UITableViewCell所做的,我们只是模仿这个结构。)
  3. 现在,您可以将内容视图的背景颜色更改为xib文件中的任何内容。
  4. Content View放置任何其他Content View
  5. 在扩展方法中重新定义contentView属性,并将IBOutlet添加到其定义中。 (请参阅下面的代码。)
  6. 将该属性与您创build的内容视图相关联,就像您使用任何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 

这个层次结构与苹果的文档一致:

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

为了摆脱这个警告,你不能设置背景颜色。 问题是当你以这种方式创buildTableHeader时,IB不提供特殊的对象。 删除源代码中的背景颜色将极好地解决所描述的问题,只需点击一下即可。

  1. 在Xcode中find头文件.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() } 

这适用于我:

 -(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文件中devise你的头文件,并在那里设置主UIView的背景,就会发生这种情况。

为了解决这个问题,将UIView背景颜色还原为InterfaceBuilder中的默认颜色,并在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.backgroundColor而不是header.backgroundColor (这是xib文件实际上在做什么)。

在willAppear委托callback中设置contentView.backgroundColor:

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

要么

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

如何使用代码来创build自定义标题并将其放置?

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