1

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

  • The UIImageView at the top should be always 2:1, so i set the Aspect ration and the Rest of the needed Constraints.

  • The 4 UIButtons should be always horizontal and should have the same Height and Width. So i worked with Equal Width and Equal Height and also with a Aspect Ratio of 1:1

  • And i have two UILabels with numberOfLines set to 0. I also made a Subclass of my UILabel, because of the preferredMaxLayoutWidth, like this:

    - (void) layoutSubviews
    {
    [super layoutSubviews];
    
    if ( self.numberOfLines == 0 )
    {
    if ( self.preferredMaxLayoutWidth != self.frame.size.width )
    {
        self.preferredMaxLayoutWidth = self.frame.size.width;
        [self setNeedsUpdateConstraints];
     }
     }
    }
    

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?

Davis
  • 1,253
  • 4
  • 17
  • 38

5 Answers5

6

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.

Community
  • 1
  • 1
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • 2
    First of all, its a shame that Apple can´t get this to work with autolayout in a "normal" way. Second: THANK YOU its really working perfectly! I will award my bounty as soon as its possible of course! – Davis Apr 30 '15 at 10:43
1

Try below solution

- (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;
}

Source:

table header view height is wrong when using auto layout, IB, and font sizes

Also refer below question...!

How do I set the height of tableHeaderView (UITableView) with autolayout?

Community
  • 1
  • 1
Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
0

My problem was that I was setting constraints on the my header view that were mapping some frame values to superview frame values. When I removed the constraints and just set the frame directly, everything worked.

nemesis
  • 1,349
  • 1
  • 15
  • 30
-3

Header view height is set in the table view delegate. By default it is the same size as in interface builder which is too large when run in app since the width for interface builder is 600.

So what you need is to implement this method in UITableViewDelegate

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 200.0 // Set the desired height based on your device or what ever you are using.
}

And Objective-C version:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200.0f;
}
Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30
  • Just to clarify, the height of the table header cannot be determined by autolayout. – Holly Apr 28 '15 at 01:45
  • I´m talking about tableHeaderView! not headerSection, theese are completely different things! – Davis Apr 28 '15 at 06:00
  • Have you tried calling `sizeToFit` on the headerView? Maybe you need to call it for the labels first. – Stefan Salatic Apr 28 '15 at 08:39
  • @Neal you are pretty much right, but wrong, It is possible with some magic. – MANIAK_dobrii Apr 30 '15 at 09:29
  • @MANIAK_dobrii You are right but for simplicity for beginners telling "mostly truth" is the best help you can offer. – Holly Apr 30 '15 at 11:07
  • @Neal I agree about beginners, but SO is here for everybody, and those non-beginners deserve to get all that subtle not-jimmy-proof details. Sometimes you need solution yesterday and you don't have time so fight SDKs and refactor your code much. – MANIAK_dobrii Apr 30 '15 at 12:07
-6
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    return 200;
}
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
Ravikanth
  • 308
  • 1
  • 10