user13386196
user13386196

Reputation:

Remove space above inset grouped UITableView

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: enter image description here 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

Answers (3)

Abdul Momen
Abdul Momen

Reputation: 407

from iOS 13.0

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return .leastNonzeroMagnitude
    }

seems works for me.

Upvotes: 0

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

Mr.OFF
Mr.OFF

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

enter image description here

And without

enter image description here

Upvotes: 0

Related Questions