How to change height of grouped UITableView header?

circuitlego picture circuitlego · Jul 17, 2013 · Viewed 63.7k times · Source

I know how to change the height of the section headers in the table view. But I am unable to find any solution to change the default spacing before the first section.

Right now I have this code:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 0;
    }
    return 10;
}

enter image description here

Answer

Mark Krenek picture Mark Krenek · May 30, 2014

Return CGFLOAT_MIN instead of 0 for your desired section height.

Returning 0 causes UITableView to use a default value. This is undocumented behavior. If you return a very small number, you effectively get a zero-height header.

Swift 3:

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }
        return tableView.sectionHeaderHeight
    }

Swift:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return CGFloat.min
    }
    return tableView.sectionHeaderHeight
}

Obj-C:

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return CGFLOAT_MIN;
    return tableView.sectionHeaderHeight;
}