How to hide first section header in UITableView grouped style
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In grouped UITableView layouts, the first section often shows a header gap even when you do not want a visible title. Hiding that first header requires both a zero-height strategy and a compatible header view implementation. Behavior also differs slightly across iOS versions, especially with top padding changes in recent releases.
Implement Header Height Logic
Use UITableViewDelegate to return a minimal height for section zero and normal height for others.
Using .leastNormalMagnitude is typically safer than raw zero for grouped style rendering behavior.
Provide Matching Header View Behavior
If you customize header views, return an empty view for first section so UIKit does not synthesize default spacing.
Returning nil for first section can still produce default behavior in some combinations of delegate methods. An explicit empty view is more predictable.
iOS 15 Plus: Section Header Top Padding
From iOS fifteen, sectionHeaderTopPadding adds extra space by default in plain-style tables, and can affect expectations during migration.
For grouped tables this setting is less central, but teams often use shared table configuration code, so include it when debugging visual spacing differences.
SwiftUI Hosting or Embedded Controllers
If your UITableViewController is embedded in another container, check surrounding layout constraints and content insets. The visible "header gap" can be top content inset from parent scroll behavior rather than section header itself.
Use the view debugger to confirm where spacing is introduced before changing delegate logic.
Test Across Dynamic Type and Empty Data
Header spacing can change when section titles are localized or when data source returns zero rows for first section. Validate these states:
- first section has rows
- first section empty, second section populated
- large text accessibility sizes
This prevents regressions where hidden headers reappear in edge conditions.
Plain Style and Grouped Style Differences
Do not copy grouped-style header logic directly into plain-style tables. UIKit spacing defaults differ, and iOS fifteen top padding behavior is more visible in plain style. Keep style-specific configuration paths so layout fixes remain predictable after UI refactors.
Common Pitfalls
- Returning zero height but not overriding
viewForHeaderInSectionconsistently. - Assuming top spacing is always section header rather than content inset.
- Forgetting iOS version differences when migrating old table view code.
- Hardcoding magic numbers for all sections without design rationale.
- Testing only one dataset shape and missing empty-section layout regressions.
Summary
- Hide first grouped-section header with minimal height and explicit empty header view.
- Keep non-first sections using normal header heights and reusable views.
- Check content insets and container layout before blaming header logic.
- Account for iOS version behavior, including top-padding changes in shared setups.
- Validate with dynamic type and empty-state datasets to avoid visual regressions.
Related reading
- How to hide keyboard in swift on pressing return key?
- How to hide keyboard in swift on pressing return key?
- How to hide keyboard when using SwiftUI?
- How to hide keyboard when using SwiftUI?
- How to hide soft keyboard on android after clicking outside EditText?
- How to hide the back button in UINavigationController?
- How to hide the keyboard when I press return key in a UITextField?
- How to hide the title bar for an Activity in XML with existing custom theme
.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.