18

I'm building my settings screen and using a grouped table view. When trying to set the headers I see spacing above my header view. I double checked and I do pass the correct view height in -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section.

Here is a screenshot of this behavior: enter image description here

You can see my view with the title (VIBRATE, SILENT MODE) in it and it's darker bg color and the brighter space above it.

Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
Gal
  • 1,582
  • 2
  • 14
  • 30

6 Answers6

73

After much searching, I have finally found a fix for this. The tableview's delegate needs to implement heightForFooterInSection and return a very small number. Returning 0 defaults to the same spacing that was causing the extra spaces.

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
Casey
  • 2,393
  • 1
  • 20
  • 22
  • 1
    Yes, the behaviour is quite incoherent. All values return the correct height but 0, which returns a default height. This is not documented and the behaviour appears to be incoherent. I opened a radar for this. – viggio24 Nov 28 '13 at 09:27
  • @viggio24 Thanks for that! Please keep us inform of the advancement. – iGranDav Nov 30 '13 at 19:09
  • @naudec Thanks. CGFLOAT_MIN is probably a better return value. I have updated my answer. – Casey Dec 02 '13 at 19:07
  • Thanks, it works for me. Returning 0 does not stop default behaviour of UITableView which renders the footer. – Bùi Thanh Hải Jun 14 '15 at 08:22
  • For anyone trying in Swift, `CGFLOAT_MIN` is unavailable, the compiler will nicely tell you to use `CGFloat.min` – apocolipse Sep 04 '15 at 23:53
5

Try this:

- (void)viewWillAppear:(BOOL)animated{
    CGRect frame = self.tableView.tableHeaderView.frame;
    frame.size.height = 1;
    UIView *headerView = [[UIView alloc] initWithFrame:frame];
    [self.tableView setTableHeaderView:headerView];
}
Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
  • Thanks for the reply. I think that this will affect the table's top header only and I'm talking about section's header. – Gal Aug 19 '13 at 07:17
  • This is actually an legit solution- The system doesn't add the extra space to the first section header if a `tableHeaderView` exists. Personally I prefer this trick over messing with footer height. – Joseph Lin Feb 06 '14 at 20:14
4

This is pretty much the same as Casey's response, however, it is a bit cleaner as it doesn't require implementing a delegate method. When you are setting up your table view, simply set the property sectionFooterHeight to 0. It accomplishes the same thing with less code (and no DBL_MIN oddness).

tableView.sectionFooterHeight = 0.0;

1

Pretty sure it is just a simple hack. But an easy way to do it is to write this function:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
      return 48.0f; // header height
}

to customize its height.

Pretty sure there are other ways to do it, that I don't know of.

Abhishek Panchal
  • 394
  • 1
  • 11
monkeyMoo
  • 173
  • 1
  • 1
  • 7
  • 3
    thanks for answering. Already have this method implemented. I return the correct header view height, but still get the extra ~20 px spacing above. – Gal Aug 19 '13 at 07:22
0

It seems that Apple made a conscious design decision to make grouped table views have extra space on top. Try adjusting the contentInset of the UITableView. See my answer here

Community
  • 1
  • 1
nvrtd frst
  • 6,272
  • 4
  • 28
  • 34
0

Swift 2.2 version:

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.min
}