iosobjective-cuitableviewnstableheaderview

tableHeaderView with Autolayout not working correctly


Hey everybody i have a TableHeaderView and everything gets managed by Autolayout:

This is my Code:

- (void)initializeHeaderViewLabels
{
 //After the Response from my server arrived i set the tableHeaderView with the Textdata
self.tableView.tableHeaderView = nil;

if((self.contentDict[@"title"]) != [NSNull null])
{
    self.headerTitleLabel.text = (self.contentDict[@"title"]);
}

if((self.contentDict[@"shortDescription"]) != [NSNull null])
{
    self.headerDescriptionLabel.text = (self.contentDict[@"shortDescription"]);
}

[self.headerView setNeedsLayout];
[self.headerView layoutIfNeeded];

CGFloat height = [self.headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = height;
self.headerView.frame = headerFrame;

[self.tableView setTableHeaderView:self.headerView];

}

I get my Image and the Text for the UILabels from my Server, so i have to wait until the Response arrives, then i call initializeHeaderViewLabels.

**My Problem is that the tableHeaderView is way too large during Runtime and so my UILabels get stretched and there is a lot of whiteSpace. Maybe i miss something?


Solution

  • In order to make it work you'll need some magic or migrate away from table header view. I've already answered on a similar question here. The trick is to magically reset tableHeaderView after evaluating it's height via autolayout. I've created sample project for that: TableHeaderView+Autolayout.