Programmatically get height of navigation bar
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, getting the navigation bar height programmatically is straightforward, but the right value depends on what you actually need. Sometimes you want only the navigation bar height. Other times you really want the full top inset, which includes the status bar or safe area and changes across devices and presentation styles.
Read the Navigation Bar Frame
If your view controller is inside a UINavigationController, the most direct value is the navigation bar's frame height.
On a standard iPhone layout, that is often 44 points. If large titles are active and expanded, the current height can be larger.
Prefer Safe Area for Layout
In many cases, what you really want is not just the bar height but the correct top layout boundary. For that, view.safeAreaInsets.top is usually the better answer.
This accounts for the status bar, notch, Dynamic Island, and navigation bar configuration automatically. For real layout work, that is often more reliable than manually adding several heights together.
Combine Status Bar and Navigation Bar Only When Necessary
If you explicitly need the total top occupied area, you can compute it from the status bar manager and navigation bar frame.
This can be useful for debugging or when working with older manual frame-based layout code. For new layout code, safe area APIs are usually better.
Timing Matters
If you query the height too early, you may get 0 or a stale value. Avoid measuring in viewDidLoad. The frame is more reliable after layout.
This matters especially when large titles, rotations, or dynamic presentation changes are involved.
Large Titles Change the Height
With large titles enabled, the navigation bar height can grow and shrink depending on scroll state.
That means there is no single permanent "navigation bar height" in all contexts. If your layout needs to react to the current state, read the current frame after the view hierarchy settles.
Use Auto Layout Instead of Manual Height Math When Possible
Often the best answer is not to read the height at all. If you pin your content to the safe area, iOS keeps the layout correct across device types and bar states.
This avoids a lot of manual calculations that become fragile later.
Know What You Are Measuring
A practical distinction is:
- want just the bar itself:
navigationController?.navigationBar.frame.height - want the usable content boundary:
view.safeAreaInsets.top - want a debugging total: status bar height plus navigation bar height
Those values are related, but they are not interchangeable.
Common Pitfalls
The most common mistake is reading the navigation bar height in viewDidLoad, before layout has happened. That can return the wrong value.
Another issue is assuming the standard height is always 44. Large titles and some layout states change the current height.
Developers also often manually add status bar and navigation bar heights when the safe area would have solved the real layout problem more robustly.
Finally, if the navigation bar is hidden, its frame information may still not represent what the user actually sees. Check the visible navigation state before using the number in layout logic.
Summary
- Use
navigationController?.navigationBar.frame.heightwhen you need the navigation bar's current height. - Use
view.safeAreaInsets.topwhen you really care about the top layout boundary. - Measure after layout, not in
viewDidLoad. - Large titles can change the current bar height dynamically.
- Prefer safe-area-based Auto Layout over manual top-offset math whenever possible.
Related reading
- Programmatically get own phone number in iOS
- Programmatically go back to previous ViewController in Swift
- Programmatically go back to previous ViewController in Swift
- Programmatically go back to the previous fragment in the backstack
- Programmatically navigate to another view controller/scene
- Programmatically obtain the phone number of the Android phone
- Programmatically open Maps app in iOS 6
- Programmatically retrieve memory usage on iPhone
.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.