Reducing the space between sections of the UITableView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Extra spacing between UITableView sections is a common layout issue in settings screens and form-heavy interfaces. The exact gap depends on table style, iOS version, and estimated height behavior, so one tweak is often not enough. A stable solution combines style choice, delegate height methods, and iOS-specific padding controls.
Core Sections
Start with table style and design intent
Section spacing behavior differs between .plain and .grouped styles. If design allows dense layout, .plain is easier to keep compact. If grouped visual blocks are required, spacing can still be reduced with explicit header and footer configuration.
Choosing style first prevents unnecessary override complexity later.
Collapse section headers and footers deliberately
Use delegate methods to return very small heights and provide empty views. Returning exact zero can trigger fallback defaults in some configurations.
This is the most reliable baseline for minimizing section gaps.
Handle iOS 15 section header top padding
On iOS 15 and newer, plain tables add default sectionHeaderTopPadding. Set it explicitly when compact spacing is required.
Without this, you may still see a top gap even with custom header heights.
Control estimated heights to avoid layout jumps
Estimated section heights can create temporary spacing jumps during reload and scrolling. Disable or minimize estimates if visual stability is critical.
Use fixed row heights only when content is predictable. For dynamic text, keep row estimation but lock section estimates.
Check inset and safe-area side effects
Sometimes the perceived section gap is actually content inset from container controllers. Inspect contentInset, adjustedContentInset, and navigation container behavior.
In complex layouts, verify spacing in both portrait and landscape because safe-area behavior changes.
Package layout settings into reusable configuration
If multiple view controllers need dense tables, create a shared setup function or subclass. This avoids inconsistent spacing behavior across screens.
A compact table configuration utility should include style defaults, padding rules, estimated height settings, and iOS 15 adjustments.
Test with realistic data volume
Spacing can look correct with one section and drift with many sections or dynamic updates. Test with multiple section counts, long text rows, and live reload scenarios.
Snapshot tests can help lock visual spacing behavior after refactors.
Distinguish visual spacing from separator rendering
Design reviews sometimes report extra spacing when the actual issue is separator inset or background contrast. Validate row separator style, section background color, and grouped container margins before changing section height logic again.
By separating these concerns, you avoid over-tuning layout code to fix what is actually a visual styling mismatch.
Common Pitfalls
- Returning zero header heights and expecting uniform behavior across styles.
- Forgetting iOS 15
sectionHeaderTopPaddingwhen using plain tables. - Confusing content inset spacing with section header spacing.
- Leaving estimated section heights enabled in dynamic update-heavy screens.
- Repeating one-off spacing tweaks instead of reusing a consistent setup.
Summary
- Start with the correct table style for your intended visual density.
- Reduce section spacing using explicit header and footer heights plus empty views.
- Set iOS 15 top padding to zero for compact plain table layouts.
- Tune estimated heights and insets to avoid visual jumps.
- Reuse one compact configuration pattern across screens for consistency.
Related reading
- Reference one string from another string in strings.xml?
- Referencing a string in a string array resource with xml
- Refresh certain row of UITableView based on Int in Swift
- Refresh devices in team provisioning profile managed by Xcode 7?
- Refreshing OAuth token using Retrofit without modifying all calls
- Refreshing UITableView Asynchronously after Core Data Loaded Swift
- Register Application class in Manifest?
- Registering for Push Notifications in Xcode 8/Swift 3.0?
.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.