Ios 如何在tableview headerView中向多行UILabel添加自动布局约束?
我一整天都在寻找解决这个问题的办法。我正试图让UILabels根据Ios 如何在tableview headerView中向多行UILabel添加自动布局约束?,ios,uitableview,uilabel,ios-autolayout,Ios,Uitableview,Uilabel,Ios Autolayout,我一整天都在寻找解决这个问题的办法。我正试图让UILabels根据tableViewHeaderView中文本的长度自动调整大小。通常,当UILabel位于UIView中时,我会设置UILabel的顶部、前导和尾随约束,它的工作方式与我所希望的一样。但是,我无法在tableViewHeaderView中实现这一点。我可以设置顶部约束和前导约束,但我的尾部约束似乎不起作用。文本超出了屏幕的宽度 将preferredMaxLayoutWidth属性设置为一个数字可以解决这个问题,但我不想硬编码 如果
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];
}
UITableView与自动布局不兼容。页眉和页脚都继承了这个问题。 正如您可能已经知道的那样,UITableView是花哨的UIScrollView。如果您曾经尝试过使用滚动视图、大量元素和自动布局,那么您必须知道它不能很好地工作。它落后了。这就是UITableView重用单元格的原因。它提高了性能,并且不使用自动布局 你应该试试这样的东西
// 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;
您是否在
awakeFromNib
中添加questionLabel
?在哪里添加约束?对于UITableView
,您的约束条件是什么?