问题
Here's how I set stuff up in my UITableViewHeaderFooterView view.
- (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;
}
I get this autolayout error:
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:
The clue is right there in the error: translatesAutoresizingMaskIntoConstraints
.
I would say you need to add the following line to your createHeader
method:
headerContainer.translatesAutoresizingMaskIntoConstraints = NO;
Here's a second idea. It looks like you want to center your label inside headerContainer, right? Instead of:
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];
Try:
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];
来源:https://stackoverflow.com/questions/22110491/correct-subclassing-and-autolayout-of-uitableviewheaderfooterview