以下是我在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>
答案 0 :(得分:2)
线索就在错误中:translatesAutoresizingMaskIntoConstraints
。
我想说您需要在createHeader
方法中添加以下行:
headerContainer.translatesAutoresizingMaskIntoConstraints = NO;
这是第二个想法。看起来你想把你的标签集中在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];
尝试:
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];