TableView Header 自动布局
TableView Header AutoLayout
尝试让我的 Table View
header 根据三个标签动态调整大小,其中一个标签具有动态内容。看起来很简单,但运气不佳。非常感谢任何建议!
按照此 post here,设置了我的约束:
而且我的代码非常简单。控制器:
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewsWithParseObject];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadViewsWithParseObject {
if (TRUE) {
self.postView.backgroundColor = [UIColor blueColor];
self.postLabel.backgroundColor = [UIColor redColor];
self.addCommentTextView.backgroundColor = [UIColor orangeColor];
self.addCommentButton.backgroundColor = [UIColor purpleColor];
}
// assign postLabel.text
self.postLabel.text = [self.postObject objectForKey:@"postText"];
[self sizeHeaderToFit];
NSLog(@"postView height = %f", self.postView.frame.size.height);
}
- (void)sizeHeaderToFit
{
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
self.tableView.tableHeaderView = header;
}
这是输出的样子(第一行是三行 post,其中 post 标签正确显示,但缺少 'add a comment' 标签;第二个是 long lorem ipsem 段落,但只有一行显示正确,同样 'add a comment' 标签被否决了):
出现此行为是因为 UITextView
没有 preferredMaxLayoutWidth
属性,所以它的 intrinsicContentSize
大小无效。
您需要手动计算 addCommentTextView
的内容高度,试试这个:
- (void)sizeHeaderToFit
{
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGFloat textViewHeight = [self.addCommentTextView sizeThatFits:CGSizeMake(self.addCommentTextView.bounds.size.width, CGFLOAT_MAX)].height;
height += textViewHeight;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
self.tableView.tableHeaderView = header;
}
编辑: 将 preferredMaxLayoutWidth
设置为您的 postLabel
将解决它。
self.postLabel.preferredMaxLayoutWidth = self.postLabel.bounds.size.width;
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGFloat textViewHeight = [self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, CGFLOAT_MAX)].height;
height += textViewHeight;
preferredMaxLayoutWidth
This property affects the size of the label when layout constraints
are applied to it. During layout, if the text extends beyond the width
specified by this property, the additional text is flowed to one or
more new lines, thereby increasing the height of the label.
尝试让我的 Table View
header 根据三个标签动态调整大小,其中一个标签具有动态内容。看起来很简单,但运气不佳。非常感谢任何建议!
按照此 post here,设置了我的约束:
而且我的代码非常简单。控制器:
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewsWithParseObject];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadViewsWithParseObject {
if (TRUE) {
self.postView.backgroundColor = [UIColor blueColor];
self.postLabel.backgroundColor = [UIColor redColor];
self.addCommentTextView.backgroundColor = [UIColor orangeColor];
self.addCommentButton.backgroundColor = [UIColor purpleColor];
}
// assign postLabel.text
self.postLabel.text = [self.postObject objectForKey:@"postText"];
[self sizeHeaderToFit];
NSLog(@"postView height = %f", self.postView.frame.size.height);
}
- (void)sizeHeaderToFit
{
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
self.tableView.tableHeaderView = header;
}
这是输出的样子(第一行是三行 post,其中 post 标签正确显示,但缺少 'add a comment' 标签;第二个是 long lorem ipsem 段落,但只有一行显示正确,同样 'add a comment' 标签被否决了):
出现此行为是因为 UITextView
没有 preferredMaxLayoutWidth
属性,所以它的 intrinsicContentSize
大小无效。
您需要手动计算 addCommentTextView
的内容高度,试试这个:
- (void)sizeHeaderToFit
{
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGFloat textViewHeight = [self.addCommentTextView sizeThatFits:CGSizeMake(self.addCommentTextView.bounds.size.width, CGFLOAT_MAX)].height;
height += textViewHeight;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
self.tableView.tableHeaderView = header;
}
编辑: 将 preferredMaxLayoutWidth
设置为您的 postLabel
将解决它。
self.postLabel.preferredMaxLayoutWidth = self.postLabel.bounds.size.width;
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGFloat textViewHeight = [self.textView sizeThatFits:CGSizeMake(self.textView.bounds.size.width, CGFLOAT_MAX)].height;
height += textViewHeight;
preferredMaxLayoutWidth
This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.