更改从xib加载的UITableViewHeaderFooterView上的背景颜色说改为使用contentView.backgroundColor [英] Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead

查看:1107
本文介绍了更改从xib加载的UITableViewHeaderFooterView上的背景颜色说改为使用contentView.backgroundColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从xib文件创建UITableViewHeaderFooterView ,几乎所有内容都正常运行。

I'm creating a UITableViewHeaderFooterView from a xib file, and almost everything is working properly.

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

The problem is that now when I try to change the background color (or if I had one configured in the xib), it will constantly output this message to the console:


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

Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

这意味着我有两个问题:

This means I have two problems:


  • 如果我不想看到那个警告,我必须摆脱xib文件中的背景颜色。(这是不可取的,因为这意味着我的xib不再反映视图将会是什么看起来像在运行时)。

  • 当我尝试通过代码更改背景颜色时,我得到了 contentView.backgroundColor 的建议,但是当我试图遵循这个建议时,没有任何反应。 (这是因为 contentView nil 。)

  • If I don't want to see that warning, I must get rid of the background color in the xib file.(This is undesirable because then it means my xib no longer reflects what the view will look like at runtime).
  • When I attempt to change the background color via code, I get the contentView.backgroundColor suggestion, but when I try to follow that suggestion, nothing happens. (This is because contentView is nil.)

注意:类似问题,但这主要涉及将消息静音,而不是找到解决上述两个问题的替代解决方案。

Note: There is a similar question here, but that was mainly concerned with muting the message, not finding an alternative solution that resolves both problems above.

更新:为了清楚起见,我想继续使用xib文件作为标题视图,并希望能够调用 dequeueReusableHeaderFooterViewWithIdentifier:以便表格可以高效它的观点管理。

Update: To be clear, I want to continue using a xib file for the header view, and want to be able to call dequeueReusableHeaderFooterViewWithIdentifier: so that the table can be efficient in its management of the views.

推荐答案

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

Here is the best way I've found to solve this:


  1. UITableViewHeaderFooterView 的背景颜色重置为 Default

  2. 直接在 UITableViewHeaderFooterView 的实例下添加一个视图,并将其命名为 Content View 。 (这正是Apple用 UITableViewCell 所做的,我们只是模仿那个结构。)

  3. 你现在可以改变背景了内容视图的颜色在xib文件中是您想要的任何颜色。

  4. 在中放置任何其他视图 内容视图

  5. 在扩展方法中重新定义 contentView 属性,并添加 IBOutlet 到它的定义。 (请参阅下面的代码。)

  6. 将属性与您创建的内容视图相关联,就像使用任何IBOutlet一样。

  7. 您现在可以更改代码中使用 contentView.backgroundColor 的背景颜色,正如错误消息告诉您的那样。

  1. Reset the background color of your UITableViewHeaderFooterView to Default.
  2. Add a single view directly underneath the instance of your UITableViewHeaderFooterView, and call it Content View. (This is exactly what Apple does with a UITableViewCell, and we are just mimicking that structure.)
  3. You can now change the background color of the content view to be whatever you want in the xib file.
  4. Place any other views inside the Content View.
  5. Redefine the contentView property in an extension method, and add IBOutlet to its definition. (See code below.)
  6. Associate the property with the content view you created, just as you would with any IBOutlet.
  7. You can now change the background color using contentView.backgroundColor in code, just as the error message tells you to.

.h文件:

@interface ABCHeaderView : UITableViewHeaderFooterView
@end

.m文件:

@interface ABCHeaderView ()

@property (nonatomic, readwrite, retain) IBOutlet UIView *contentView;

@end

@implementation ABCHeaderView

@synthesize contentView;

@end

此层次结构与 Apple的文档


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

If you have custom content to display, create the subviews for your content and add them to the view in the contentView property.

这篇关于更改从xib加载的UITableViewHeaderFooterView上的背景颜色说改为使用contentView.backgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆