更改从xib加载的UITableViewHeaderFooterView上的背景颜色说改为使用contentView.backgroundColor [英] Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead
问题描述
我从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 becausecontentView
isnil
.)
注意:有类似问题,但这主要涉及将消息静音,而不是找到解决上述两个问题的替代解决方案。
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:
- 将
UITableViewHeaderFooterView
的背景颜色重置为 Default 。 - 直接在
UITableViewHeaderFooterView
的实例下添加一个视图,并将其命名为 Content View 。 (这正是Apple用UITableViewCell
所做的,我们只是模仿那个结构。) - 你现在可以改变背景了内容视图的颜色在xib文件中是您想要的任何颜色。
- 在中放置任何其他视图
内容视图
。 - 在扩展方法中重新定义
contentView
属性,并添加IBOutlet
到它的定义。 (请参阅下面的代码。) - 将属性与您创建的内容视图相关联,就像使用任何IBOutlet一样。
- 您现在可以更改代码中使用
contentView.backgroundColor
的背景颜色,正如错误消息告诉您的那样。
- Reset the background color of your
UITableViewHeaderFooterView
to Default. - Add a single view directly underneath the instance of your
UITableViewHeaderFooterView
, and call it Content View. (This is exactly what Apple does with aUITableViewCell
, and we are just mimicking that structure.) - You can now change the background color of the content view to be whatever you want in the xib file.
- Place any other views
inside
theContent View
. - Redefine the
contentView
property in an extension method, and addIBOutlet
to its definition. (See code below.) - Associate the property with the content view you created, just as you would with any IBOutlet.
- 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屋!