如何将自动布局约束添加到 tableview headerView 中的多行 UILabel?

How to add autolayout constraints to a multi-line UILabel inside tableview headerView?

我整天都在寻找解决这个问题的方法。我正在尝试根据 tableViewHeaderView 中文本的长度让我的 UILabel 自动调整自己的大小。通常,在 UIView 中使用我的 UILabel,我会为 UILabel 设置顶部、前导和尾随约束,它会像我想要的那样工作。但是,我无法在 tableViewHeaderView 中使用它。我能够设置顶部和前导约束,但我的尾随约束似乎不起作用。文字超出屏幕宽度。

preferredMaxLayoutWidth 属性 设置为一个数字可以解决问题,但我不想硬编码。

如果我错了请纠正我,但是设置前导和尾随约束应该能够给我视图的宽度,不是吗?然后我可以用那个值设置 preferredMaxLayoutWidth 。但该值是 2403,比屏幕的宽度长得多。

还有其他人遇到过这种情况吗?

#import "CustomTableViewHeader.h"

@implementation ReplyHeader{
    UILabel *questionLabel;
}



questionLabel = [[UILabel alloc]init];
            questionLabel.text = @"SAMPLE TEXT: I had a question about how I can be a better human being using your method. I belive it is an integral part of what it means to be a human so I want to learn more if you are able give more details about it. I also found that what you said about the dogs out there is very cool and would love to learn more about that.";
            questionLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:17];
        questionLabel.lineBreakMode = NSLineBreakByWordWrapping;
        questionLabel.numberOfLines = 0;
        questionLabel.translatesAutoresizingMaskIntoConstraints = NO;

        [self addSubview:questionLabel];

约束条件

  [self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f]];

            [self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:10]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-10]];

更新视图

- (void)viewDidLayoutSubviews
{
    questionLabel.preferredMaxLayoutWidth = questionLabel.frame.size.width;
    [self layoutIfNeeded];
}

UITableView 与 AutoLayout 不兼容。页眉和页脚都继承了这个问题。 你可能已经知道,UITableViews 是花哨的 UIScrollViews。如果您曾经尝试过使用滚动视图、大量元素和自动布局,您一定知道它不能很好地工作。它滞后。这就是 UITableView 重用单元格的原因。它提高了性能并且他们不使用 AutoLayout。

你应该试试

 // with this you will get the most compressed size for your view if the constraints properly define it's size
 CGFloat height = [footer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
 footer = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(footer.frame), CGRectGetWidth(footer.bounds), height);
 // This will tell the layout engine to ignore the view and not try to resize it
 view.autoresizingMask = UIViewAutoresizingNone; 
 self.tableView.tableFooterView = footer;