Correct subclassing and autolayout of UITableViewHeaderFooterView

亡梦爱人 提交于 2020-01-05 08:42:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!