How do I set the height of tableHeaderView UITableView with autolayout?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
tableHeaderView is a little awkward with Auto Layout because UITableView does not automatically resize the header based on its constraints. The usual fix is to let Auto Layout compute the header's fitting size, update the header frame manually, and then assign it back to tableView.tableHeaderView.
Why the Header Does Not Resize Automatically
The cells in a table view can participate in self-sizing much more naturally than the table header. tableHeaderView is just a plain UIView attached above the first section, and the table view mainly respects its frame, not its internal constraints.
That means two things must both be true:
- the header's internal subviews need complete constraints,
- you still need to convert that Auto Layout result into a concrete frame height.
If you only add constraints and never update the frame, the header often appears with the wrong height.
The Standard Pattern
Create the header view, configure its constraints, force layout, measure the fitting height, update the frame, and then reassign it.
Two details matter a lot here:
- '
systemLayoutSizeFittingasks Auto Layout for the correct compressed height.' - reassigning
tableView.tableHeaderViewtells the table view to respect the updated frame.
Why viewDidLayoutSubviews Is Useful
The header width often depends on the table view's final bounds. During viewDidLoad, those bounds may not yet be final, especially during rotation or initial layout.
Calling the size update in viewDidLayoutSubviews ensures the header is measured using the actual table width. The if header.frame.height != newSize.height guard prevents endless reassignment loops.
Constraint Requirements Inside the Header
Auto Layout can only compute a height if the header has enough information. Typical mistakes include:
- missing bottom constraints,
- subviews with ambiguous widths,
- labels that need
numberOfLines = 0but were left at one line, - content that depends on a width not yet assigned.
If the internal constraints are incomplete, systemLayoutSizeFitting returns an unhelpful value.
Dynamic Content Updates
If the header content changes after the table first appears, call the same sizing method again.
This is common when the header shows fetched text, localization changes, or an expandable summary.
Common Pitfalls
The most common mistake is expecting tableHeaderView to self-size the way modern table-view cells do. It does not. You must update the frame manually.
Another issue is forgetting to assign the header back to tableView.tableHeaderView after changing its height. Updating the frame alone is often not enough to trigger the table view to honor the new size.
Developers also sometimes calculate the fitting height before the table view has its final width, which produces the wrong result for wrapping labels.
Summary
- '
tableHeaderViewuses its frame height, not just Auto Layout constraints.' - Build correct internal constraints first so the header has a measurable size.
- Use
systemLayoutSizeFittingto compute the height. - Reassign
tableView.tableHeaderViewafter updating the frame. - Recalculate in
viewDidLayoutSubviewsand after dynamic content changes.
Related reading
- How do I set the size of a SF Symbol in SwiftUI?
- How do I set up a simple delegate to communicate between two view controllers?
- How do I set up IntelliJ IDEA for Android applications?
- How do I set up IntelliJ IDEA for Android applications?
- How do I shake an Android device within the Android emulator to bring up the dev menu to debug my React Native app
- How do I show the number keyboard on an EditText in android?
- How do I show/hide a UIBarButtonItem?
- How do I shuffle an array in Swift?
.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.