Ios UITableViewHeaderFooterView的正确子类化和自动布局

Ios UITableViewHeaderFooterView的正确子类化和自动布局,ios,uitableview,ios7,autolayout,Ios,Uitableview,Ios7,Autolayout,以下是我如何在UITableViewHeaderFooterView视图中设置内容 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self.contentView addSubview:self.createHeader]; } return self; } - (UIView *)createHeader { UIView *headerContainer

以下是我如何在UITableViewHeaderFooterView视图中设置内容

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    [self.contentView addSubview:self.createHeader];
}
return self;
}

- (UIView *)createHeader
{
  UIView *headerContainer = [[UIView alloc] init];
  headerContainer.backgroundColor = [UIColor blackColor];
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  label.translatesAutoresizingMaskIntoConstraints = NO;
  label.text = @"titlename";

  [headerContainer addSubview:label];

  NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
                                                                   attribute:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              attribute:NSLayoutAttributeLeft
                                                             multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeTrailing
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          attribute:NSLayoutAttributeRight
                                         multiplier:1.f constant:-16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeTop
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          attribute:NSLayoutAttributeTop
                                         multiplier:1.f constant:0.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeBottom
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:headerContainer
                                          attribute:NSLayoutAttributeBottom
                                         multiplier:1.f constant:0.f];
[headerContainer addConstraint:constraint];

return headerContainer;
}
我遇到了以下自动布局错误:

 Probably at least one of the constraints in the following list is one you don't want. 
 Try this: (1) look at each constraint and try to figure out which you don't expect; 
 (2) find the code that added the unwanted constraint or constraints and fix it. 
 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand,
 refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(
"<NSLayoutConstraint:0xd4bd7e0 UILabel:0x17254980.leading == UIView:0x172547d0.left + 16>",
"<NSLayoutConstraint:0xd4bd810 UILabel:0x17254980.trailing == UIView:0x172547d0.right - 16>",
"<NSAutoresizingMaskLayoutConstraint:0xd402510 h=--& v=--& H:[UIView:0x172547d0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xd4bd810 UILabel:0x17254980.trailing == UIView:0x172547d0.right - 16>
下面列表中可能至少有一个约束是您不想要的约束。
试着这样做:(1)看看每个约束,试着找出你不期望的约束;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到您不了解的NSAutoresizingMaskLayoutConstraints,
请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档
(
"",
"",
""
)
将尝试通过打破约束进行恢复

错误提示就在那里:
翻译自动调整大小GMaskintoConstraints

我想说,您需要在
createHeader
方法中添加以下行:

headerContainer.translatesAutoresizingMaskIntoConstraints=否

还有第二个想法。看起来你想把你的标签放在headerContainer的中心,对吗?而不是:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
                                                                   attribute:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              attribute:NSLayoutAttributeLeft
                                                             multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeTrailing
                                          relatedBy: NSLayoutRelationLessThanOrEqual
                                             toItem:headerContainer
                                          attribute:NSLayoutAttributeRight
                                         multiplier:1.f constant:-16.f];
[headerContainer addConstraint:constraint];
尝试:


完成此操作后,错误消失,但标题的位置已关闭。是的,我调整了答案。在视图上设置该属性时,通常需要添加一些约束,以弥补禁用translatesAutoresizingMaskIntoConstraints时丢失的约束。标题的位置是否在superview中关闭了?很酷,谢谢!你知道如何找到默认约束吗?我想你不想要默认约束。这就是造成你问题的原因。您希望为headerContainer的大小和位置添加一些附加约束。我建议大家阅读视觉格式语言,因为它非常适合指定这些类型的约束。
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label
                                                                   attribute:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:headerContainer
                                                              attribute:NSLayoutAttributeLeft
                                                             multiplier:1.f constant:16.f];
[headerContainer addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeTrailing
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:label
                                          attribute:NSLayoutAttributeLeading
                                         multiplier:1.f constant:0];
[headerContainer addConstraint:constraint];