1

I have set up a UITableViewController with a Container View as header view in Interface Builder. The header view has a height of 180px:

enter image description here

Unfortunately, at run-time the height of the Container View is just 116px:

enter image description here

If I replace the Container View in the table head with a standard view, say UILabel, the height of the header view I set in Interface Builder is correctly honoured. But this somehow doesn't work with Container Views.

Adjust Scroll View Insets doesn't help because it just moves the overflowing part of the image under the Navigation Bar, the height of the Container view is still wrong.

Any ideas? Thanks!

Tobi Kremer
  • 618
  • 6
  • 22
  • After a day of poking around, I gave up and implemented the header as a custom view. Setting it as the tableHeaderView worked perfectly fine, so I guess it's a problem of iOS not being able to determine the correct height of container views at run-time. – Tobi Kremer Aug 14 '15 at 07:29

1 Answers1

1

You should implement UITableViewDelegate and declare the method

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 180;
}

Reference: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForHeaderInSection:

Lai Xin Chu
  • 2,462
  • 15
  • 29
  • Thanks, but that doesn't help. It correctly sets the height for all headers, except the Container View in the table header. – Tobi Kremer Aug 13 '15 at 09:41
  • I notice that your header isn't implemented using a prototype cell; rather it is just a view above the tableview. In that case, can't you just add a height constraint of 180 to it? – Lai Xin Chu Aug 13 '15 at 09:44
  • Exactly! I'm afraid it's not possible to add constraints to a Table Header View defined in Interface Builder. – Tobi Kremer Aug 13 '15 at 09:47
  • you could use a ViewController, define a container view, then a tableview below it. it would be pretty much the same as using a UITableViewController. – Lai Xin Chu Aug 13 '15 at 10:07
  • Alternatively, you can define your header view as a prototype cell, then use the UITableViewDelegate methods to insert the header view for the first section only. – Lai Xin Chu Aug 13 '15 at 10:07
  • Both approaches have their drawbacks AFAICT: With the UIViewController solution I'll lose scrolling, because the container view will be stuck at the top. The second approach will probably give me headaches when trying to align the header right below the navigation bar and make it scroll underneath it. I've been there and remember it as being very painful. Using a UITableViewController solves most issues out of the box and is pretty straightforward, but something seems to be special when it comes to container views as tableviewheaders ... – Tobi Kremer Aug 13 '15 at 11:12