Gal
Gal

Reputation: 1582

Header views height for grouped UITableView on iOS7

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.

Upvotes: 18

Views: 16444

Answers (6)

Lucas Novaes
Lucas Novaes

Reputation: 76

Swift 2.2 version:

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

Upvotes: 0

Michael DiStefano
Michael DiStefano

Reputation: 594

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;

Upvotes: 4

Casey
Casey

Reputation: 2403

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

Upvotes: 73

monkeyMoo
monkeyMoo

Reputation: 173

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.

Upvotes: 1

nvrtd frst
nvrtd frst

Reputation: 6342

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

Upvotes: 0

Shekhar Gupta
Shekhar Gupta

Reputation: 6218

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

Upvotes: 5

Related Questions