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

时间:2017-01-18 02:52:02

标签: ios uitableview uilabel ios-autolayout

我整天都在寻找解决这个问题的方法。我试图让我的UILabels根据tableViewHeaderView内的文本长度自动调整大小。通常情况下,我的UILabel在UIView中,我会为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];
}

1 个答案:

答案 0 :(得分:0)

UITableView与AutoLayout不相容。页眉和页脚都继承了这个问题。 您可能已经知道,UITableViews是花哨的UIScrollViews。如果您曾尝试使用滚动视图,许多元素和AutoLayout,您必须知道它不能正常工作。它滞后了。这就是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;