Reputation: 3225
I made the tableview .grouped so that the headers would not be sticky. However, that results in adding extra padding at the top and bottom of the tableview (which I fixed), as well as extra space on top of my header views. Online it seems like the only thing ppl are talking about is getting rid of the header, but the header is why I made it grouped as that stops it from sticking. How do I get rid of the 20 or so pixels of padding that .grouped adds to all headers without getting rid of my header views? Undoing the .grouped setting gets rid of the white space. Is that the only way as I want to keep it grouped?
You'd think Apple would make this easier.
I colored my header red to highlight the unwanted white space above. Changing the height for row at of the cell does not affect the white area.
Upvotes: 2
Views: 1798
Reputation: 139
This works for me, put this code inside the viewDidLoad() tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
Example:
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView()
}
func setupTableViews() {
tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
}
Upvotes: 0
Reputation: 1630
This will get rid of the headerView and footerView for the overall tableView:
tableView.tableHeaderView = UIView(frame: CGRect(x: CGFloat.leastNonzeroMagnitude, y: CGFloat.leastNonzeroMagnitude, width: tableView.bounds.size.width, height: CGFloat.leastNonzeroMagnitude))
tableView.tableFooterView = UIView(frame: CGRect(x: CGFloat.leastNonzeroMagnitude, y: CGFloat.leastNonzeroMagnitude, width: tableView.bounds.size.width, height: CGFloat.leastNonzeroMagnitude))
Then to individually set the height to 0 for the section headers, you have to use the following function:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
And for the footer per section, similarly:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
Upvotes: 1