Remove empty space before cells in UITableView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The blank area before the first UITableViewCell is usually not random layout drift. It is almost always one of a small set of causes: a section header, automatic content inset adjustment, grouped-style padding, or a header view you forgot was there.
Identify the source of the gap
Before changing properties blindly, determine what kind of space you are seeing.
- If the gap is above the first section and scrolls with the table, suspect
tableHeaderViewor content inset. - If the gap looks like section spacing, suspect header height defaults.
- If it only appears on newer iOS versions, check
sectionHeaderTopPadding. - If the table is under a navigation bar, automatic safe-area adjustment may be involved.
That distinction matters because the fix for one cause can make another layout worse.
A reliable baseline configuration
The following UITableViewController setup removes the common top spacing sources while keeping the table behavior predictable.
Two details are easy to miss. First, a grouped table can still reserve header space even when you think the header height is zero. Using .leastNormalMagnitude is more reliable than returning plain zero in those cases. Second, iOS 15 introduced extra top padding for section headers, so sectionHeaderTopPadding = 0 is often the missing fix.
When content insets are the real problem
If the table view sits under a navigation bar or inside a container controller, the top gap may come from automatic inset adjustment rather than headers. In that case, inspect contentInsetAdjustmentBehavior and the surrounding layout.
You can turn that behavior off, but do it only if you control the full-screen layout and understand the tradeoff.
This removes the automatic safe-area compensation, which can be correct for edge-to-edge designs and incorrect for standard navigation layouts. If your rows end up under the navigation bar, the fix was too aggressive.
Debugging the issue quickly
The fastest way to debug is to strip the table down:
- set
tableHeaderViewtonilor a zero-sized view - return
nilfor section headers - set header heights explicitly
- inspect content inset values at runtime
Once the gap disappears, add back only the behavior you actually need. That is more reliable than stacking one workaround on top of another.
Common Pitfalls
- Returning zero for a grouped section header and expecting all spacing to disappear.
- Forgetting
sectionHeaderTopPaddingon iOS 15 and later. - Clearing content insets when the real source is a section header.
- Disabling automatic inset adjustment without checking how the table sits under bars and safe areas.
- Leaving an empty
tableHeaderViewattached and overlooking it during debugging.
Summary
- Top space before the first cell usually comes from headers, content insets, or iOS default padding.
- Start by checking
tableHeaderView, section header heights, andsectionHeaderTopPadding. - Use
.leastNormalMagnitudefor section header height when plain zero does not remove the gap. - Change
contentInsetAdjustmentBehavioronly when the layout truly requires it. - Diagnose by removing one spacing source at a time instead of layering random fixes.
Related reading
- remove grey background on link clicked in ios safari / chrome / firefox
- Remove HTML Tags from an NSString on the iPhone
- Remove iOS input shadow
- Remove last character from string. Swift language
- Remove or uninstall library previously added cocoapods
- Remove println for release version iOS Swift
- Remove shadow below actionbar
- Remove text from Back button keeping the icon
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.