Reputation: 279
Trying to get my Table View
header to resize dynamically based off of three labels, one of which has dynamic content. Seems quite simple enough, but not having much luck. Any suggestions greatly appreciated!
Following this post here, have setup my constraints as such:
And my code is quite simple. Controller:
- (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;
}
This is what the output looks like (first is three line post where post label appears correctly but the 'add a comment' label is missing; second is long lorem ipsem paragraph but only one line is showing correctly and likewise the 'add a comment' label is being overruled):
Upvotes: 0
Views: 710
Reputation: 10479
This behavior happens because UITextView
not have preferredMaxLayoutWidth
property so its intrinsicContentSize
is an invalid size.
You need to calculate the content height of addCommentTextView
manually, try this:
- (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;
}
EDIT: Set preferredMaxLayoutWidth
to your postLabel
that will resolve it.
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.
Upvotes: 1