Ios 更改从xib加载的UITableViewHeaderFooterView上的背景色表示改为使用contentView.backgroundColor
我很好,几乎一切都正常工作 问题是,现在当我尝试更改背景颜色时(或者如果我在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文件中的背景色(这是不可取的,因为这意味着我的xib不再反映运行时视图的样子)
- 当我试图通过代码更改背景颜色时,我得到了
建议,但当我尝试遵循该建议时,什么也没有发生。(这是因为contentView.backgroundColor
是contentView
)nil
更新:说得很清楚,我想继续使用xib文件作为标题视图,并希望能够调用,以便表能够有效地管理视图。以下是我找到的解决此问题的最佳方法:
UITableViewHeaderFooterView
的背景色重置为默认值UITableViewHeaderFooterView
实例的正下方添加一个视图,并将其称为内容视图。(这正是苹果使用UITableViewCell
所做的,我们只是在模仿这种结构。)放在内容视图内
contentView
属性,并将IBOutlet
添加到其定义中。(请参见下面的代码。)contentView.backgroundColor
更改背景色,正如错误消息告诉您的那样@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并没有为此提供特殊的对象删除源代码中的背景色将很好地解决上述问题,只需单击几下即可
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
}