Day of Swift: Disable sticky Table header

Mark
Mark
Sep 2, 2018 · 1 min read

As title, there are several ways to do that.

Set the UITableView style as grouped

let tableview = UITableView(frame: CGRect.zero, 
style: .grouped)

Custom UITableView contentInset

let tableHeaderHeight = CGFloat(40)
let tableWidth = self.tableView.bounds.size.width
let tableFrame = CGRect(x: 0, y: 0, width: tableWidth , height: tableHeaderHeight)
self.tableView.tableHeaderView = UIView(frame: tableFrame)
// set the content insets top value
self.tableView.contentInset = UIEdgeInsetsMake(-tableHeaderHeight, 0, 0, 0)

It sets the tableview contentInset top as navigate value. The table header will still sticky on the top but the user won’t see that due to the space in content inset top is set same as header height.

Use UITableViewCell as Header

The last way is using cellForRow indexPath:.

But this way will increase the complexity of your data source management when you need to reload specified row or update one of the cell data.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade