Reputation:
I have a table view controller with a navigation bar. My table view is an inset grouped table view, and i have headers for each section. I'm trying to remove the space above the header of the table view. Here is my table view:
How can I do that?
I've tried:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
But this doesn't work.
Upvotes: 2
Views: 2107
Reputation: 407
from iOS 13.0
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
seems works for me.
Upvotes: 0
Reputation: 41
For anyone who's still searching for an answer. That padding is caused by the UITableView's own header. This can be set with the tableHeaderView attribute. So it will look like this:
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 10, height: 100), style: .grouped)
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
Settings the height to zero will cause the default height behaviour and not the actual size to be 0.
Upvotes: 4
Reputation: 108
try to remove the method viewForHeaderInSection from your tableviewcontroller It should help you.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
With this method, it looks like
And without
Upvotes: 0