0

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:enter image description here

enter image description here

enter image description here

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):

enter image description here

enter image description here

3
  • Give height constraint to addCommentTextView Commented Jun 2, 2015 at 3:59
  • Try to remove bottom constraint for Add Comment text view and set its number of lines to 0 in attribute inspector. Commented Jun 2, 2015 at 4:50
  • @Uttam Adding a height constraint almost fixes it, but still cuts off the second example by a bit i.imgur.com/z5Ir0uL.png and i.imgur.com/tL1jlY3.png Commented Jun 2, 2015 at 12:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Seems to have fixed the first example: i.imgur.com/l2lNv24.png but not the second i.imgur.com/hvPQ9gn.png, any idea why?
That solved it, thank you! Have marked answer as accepted. Could I ask you one final post explaining why you also had to set the preferredMaxLayoutWidth for the postLabel as well, given you mentioned the UITextView needed it since the intrinsicContentSize was invalid, but does this also apply to the UILabel as well?
The Auto Layout always needs enough constraints to determine the size and position of the all views, however you don't need to set the height constraint for UILabel because the UILabel can calculate its height(or intrinsicContentSize) with its content and the preferredMaxLayoutWidth. If you never set the label's preferredMaxLayoutWidth so the intrinsicContentSize of the label will not be what you're expecting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.