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

Community
  • 1
  • 1
SimplyLearning
  • 269
  • 3
  • 11

1 Answers1

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.

Bannings
  • 10,376
  • 7
  • 44
  • 54
  • Seems to have fixed the first example: http://i.imgur.com/l2lNv24.png but not the second http://i.imgur.com/hvPQ9gn.png, any idea why? – SimplyLearning Jun 02 '15 at 12:14
  • 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? – SimplyLearning Jun 03 '15 at 01:24
  • 1
    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 – Bannings Jun 03 '15 at 02:49