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

68,734

Solution 1

You need to use the UIView systemLayoutSizeFittingSize: method to obtain the minimum bounding size of your header view.

I provide further discussion on using this API in this Q/A:

How to resize superview to fit all subviews with autolayout?

Solution 2

I've found an elegant way to way to use auto layout to resize table headers, with and without animation.

Simply add this to your View Controller.

func sizeHeaderToFit(tableView: UITableView) {
    if let headerView = tableView.tableHeaderView {
        let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        var frame = headerView.frame
        frame.size.height = height
        headerView.frame = frame
        tableView.tableHeaderView = headerView
        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()
    }
}

To resize according to a dynamically changing label:

@IBAction func addMoreText(sender: AnyObject) {
    self.label.text = self.label.text! + "\nThis header can dynamically resize according to its contents."
}

override func viewDidLayoutSubviews() {
    // viewDidLayoutSubviews is called when labels change.
    super.viewDidLayoutSubviews()
    sizeHeaderToFit(tableView)
}

To animate a resize according to a changes in a constraint:

@IBOutlet weak var makeThisTallerHeight: NSLayoutConstraint!

@IBAction func makeThisTaller(sender: AnyObject) {

    UIView.animateWithDuration(0.3) {
        self.tableView.beginUpdates()
        self.makeThisTallerHeight.constant += 20
        self.sizeHeaderToFit(self.tableView)
        self.tableView.endUpdates()
    }
}

See the AutoResizingHeader project to see this in action. https://github.com/p-sun/Swift2-iOS9-UI

AutoResizingHeader

Solution 3

I really battled with this one and plonking the setup into viewDidLoad didn't work for me since the frame is not set in viewDidLoad, I also ended up with tons of messy warnings where the encapsulated auto layout height of the header was being reduced to 0. I only noticed the issue on iPad when presenting a tableView in a Form presentation.

What solved the issue for me was setting the tableViewHeader in viewWillLayoutSubviews rather than in viewDidLoad.

func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        if tableView.tableViewHeaderView == nil {
            let header: MyHeaderView = MyHeaderView.createHeaderView()
            header.setNeedsUpdateConstraints()
            header.updateConstraintsIfNeeded()
            header.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), CGFloat.max)
            var newFrame = header.frame
            header.setNeedsLayout()
            header.layoutIfNeeded()
            let newSize = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
            newFrame.size.height = newSize.height
            header.frame = newFrame
            self.tableView.tableHeaderView = header
        }
    }

Solution 4

This solution resizes the tableHeaderView and avoids infinite loop in the viewDidLayoutSubviews() method I was having with some of the other answers here:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if let headerView = tableView.tableHeaderView {
        let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
        var headerFrame = headerView.frame

        // comparison necessary to avoid infinite loop
        if height != headerFrame.size.height {
            headerFrame.size.height = height
            headerView.frame = headerFrame
            tableView.tableHeaderView = headerView
        }
    }
}

See also this post: https://stackoverflow.com/a/34689293/1245231

Solution 5

Your solution using systemLayoutSizeFittingSize: works if the header view is just updated once on each view appearance. In my case, the header view updated multiple times to reflect status changes. But systemLayoutSizeFittingSize: always reported the same size. That is, the size corresponding to the first update.

To get systemLayoutSizeFittingSize: to return the correct size after each update I had to first remove the table header view before updating it and re-adding it:

self.listTableView.tableHeaderView = nil;
[self.headerView removeFromSuperview];
Share:
68,734
Andrew Cross
Author by

Andrew Cross

Updated on August 26, 2020

Comments

  • Andrew Cross
    Andrew Cross over 3 years

    I'm been smashing my head against the wall with this for last 3 or 4 hours and I can't seem to figure it out. I have a UIViewController with a full screen UITableView inside of it (there's some other stuff on the screen, which is why I can't use a UITableViewController) and I want to get my tableHeaderView to resize with autolayout. Needless to say, it's not cooperating.

    See screenshot below.

    enter image description here

    Because the overviewLabel (e.g. the "List overview information here." text) has dynamic content, I'm using autolayout to resize it and it's superview. I've got everything resizing nicely, except for the tableHeaderView, which is right below Paralax Table View in the hiearchy.

    The only way I've found to resize that header view is programatically, with the following code:

    CGRect headerFrame = self.headerView.frame;
    headerFrame.size.height = headerFrameHeight;
    self.headerView.frame = headerFrame;
    [self.listTableView setTableHeaderView:self.headerView];
    

    In this case, headerFrameHeight is a manual calculation of the tableViewHeader height as follows (innerHeaderView is the white area, or the second "View", headerView is tableHeaderView):

    CGFloat startingY = self.innerHeaderView.frame.origin.y + self.overviewLabel.frame.origin.y;
    CGRect overviewSize = [self.overviewLabel.text
                           boundingRectWithSize:CGSizeMake(290.f, CGFLOAT_MAX)
                           options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{NSFontAttributeName: self.overviewLabel.font}
                           context:nil];
    CGFloat overviewHeight = overviewSize.size.height;
    CGFloat overviewPadding = ([self.overviewLabel.text length] > 0) ? 10 : 0; // If there's no overviewText, eliminate the padding in the overall height.
    CGFloat headerFrameHeight = ceilf(startingY + overviewHeight + overviewPadding + 21.f + 10.f);
    

    The manual calculation works, but it's clunky and prone to error if things change in the future. What I want to be able to do is have the tableHeaderView auto-resize based on the provided constraints, like you can anywhere else. But for the life of me, I can't figure it out.

    There's several posts on SO about this, but none are clear and ended up confusing me more. Here's a few:

    It doesn't really make sense to change the translatesAutoresizingMaskIntoConstraints property to NO, since that just causes errors for me and doesn't make sense conceptually anyway.

    Any help would really be appreciated!

    EDIT 1: Thanks to TomSwift's suggestion, I was able to figure it out. Instead of manually calculating the height of the overview, I can have it calculated for me as follows and then re-set the tableHeaderView as before.

    [self.headerView setNeedsLayout];
    [self.headerView layoutIfNeeded];
    CGFloat height = [self.innerHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + self.innerHeaderView.frame.origin.y; // adding the origin because innerHeaderView starts partway down headerView.
    
    CGRect headerFrame = self.headerView.frame;
    headerFrame.size.height = height;
    self.headerView.frame = headerFrame;
    [self.listTableView setTableHeaderView:self.headerView];
    

    Edit 2: As others have noted, the solution posted in Edit 1 doesn't seem to work in viewDidLoad. It does, however, seem to work in viewWillLayoutSubviews. Example code below:

    // Note 1: The variable names below don't match the variables above - this is intended to be a simplified "final" answer.
    // Note 2: _headerView was previously assigned to tableViewHeader (in loadView in my case since I now do everything programatically).
    // Note 3: autoLayout code can be setup programatically in updateViewConstraints.
    - (void)viewWillLayoutSubviews {
        [super viewWillLayoutSubviews];
    
        [_headerWrapper setNeedsLayout];
        [_headerWrapper layoutIfNeeded];
        CGFloat height = [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
        CGRect headerFrame = _headerWrapper.frame;
        headerFrame.size.height = height;
        _headerWrapper.frame = headerFrame;
        _tableView.tableHeaderView = _headerWrapper;
    }
    
  • Andrew Cross
    Andrew Cross over 10 years
    It's coming back with height = 0, although I'm not 100% sure I've configured it right since this isn't a UITableViewCell, so there isn't the contentView to call "systemLayoutSizeFittingSize" on. What I tried: [self.headerView setNeedsLayout]; [self.headerView layoutIfNeeded]; CGFloat height = [self.headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].h‌​eight; CGRect headerFrame = self.headerView.frame; headerFrame.size.height = height; self.headerView.frame = headerFrame; [self.listTableView setTableHeaderView:self.headerView]; I also confirmed that preferredMax... is valid.
  • Andrew Cross
    Andrew Cross over 10 years
    Aha! Figured it out, I needed to do [self.innerHeaderView systemLayoutSizeFittingSize...] instead of self.headerView as that's what has the actual dynamic content. Thanks a ton!
  • Bonnke
    Bonnke over 7 years
    Hey p-sun. Thanks for great example. Seems I have issue with tableViewHeader. I tried to have the same constraints like in your example, but not working. How exactly I should add constraints to label which i want to increase height ? Thanks for any suggestion
  • p-sun
    p-sun over 7 years
    Make sure you hook the label you want to make taller to @IBOutlet makeThisTaller and @IBAction fun makeThisTaller like in the example. Also, constrain all sides of your label to the tableViewHeader (e.g. top, bottom, left, and right).
  • Bonnke
    Bonnke over 7 years
    Thanks a lot! Finally solved by adding this line: lblFeedDescription.preferredMaxLayoutWidth = lblFeedDescription.bounds.width where label is that one which I want to increase size. Thanks !
  • bio
    bio almost 6 years
    I am using this method too. Think it’s the best because there is no fiddling with constraints. It’s also very compact.
  • bio
    bio almost 6 years
    It works but in my app there is a small height discrepancy. Probably because my labels are using an attributed text and Dynamic Type.
  • Andy Weinstein
    Andy Weinstein almost 4 years
    Thanks! If you're doing this inside a View instead of a ViewController, then instead of overriding viewDidLayoutSubviews, you can override layoutSubviews
  • Sourabh Sharma
    Sourabh Sharma over 3 years
    for updated solution with minimal code try this : stackoverflow.com/a/63594053/3933302