Member-only story
Mastering UITableView Content Insets
When, why and how
UITableView
is a fundamental iOS component, but managing it can be tricky.
Dealing with extra spacing, floating buttons and ensuring content isn’t covered by UI elements can be challenging.
One of the tools in our development toolkit is contentInset
and I’ll cover that in this article.
I’ll put a demo project on https://github.com/stevencurtis/SwiftCoding/tree/master/DemoContentInserts, and I hope that helps things for you too.
Understanding contentInset
The contentInset
property of UIScrollView
(which UITableView
inherits) allows you to add extra padding around the scrollable content without affecting the table’s constraints.
Example:
tableView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
Ensures that the content starts *lower* than the default position (the top inset).
In the following image the background of the UITableView
is set to .green
so you can see the inset at the top of the UITableView
.
The thing is you wouldn’t usually be able to see that inset at the top of the table unless you scroll to see it. If you *initially* want the inset to be shown at…