Peter Pik
Peter Pik

Reputation: 11193

UITableView headerView different sizes

I'm trying to apply a size on my UITableView headerView but it seems like the first section header always is a bit smaller than the rest section headerViews. I've said the constraints for the UITableView so it should be fine. How come the first section is smaller?

enter image description here

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return ""
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 20
}

Upvotes: 1

Views: 878

Answers (1)

Ron Fessler
Ron Fessler

Reputation: 2789

This behavior occurs when using a Grouped UITableView. The section header height appears shorter for the first section header, because it doesn't have a footer above of it. However, every section header after the first one, does have a footer directly above it. The footers have a default height, which is contributing to the overall height appearance of the section header. Therefore, set the section footer heights so the space between the group sections will have the same visual height.

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

We use CGFloat.min instead of 0, because returning 0 will cause the footer to return a default height.

Upvotes: 7

Related Questions