Navigation Bar
Programmatic Access
UI Development
Mobile App Design
Software Engineering

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.

Browse interview questions

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.

swift
1if let navBar = navigationController?.navigationBar {
2    let navBarHeight = navBar.frame.height
3    print(navBarHeight)
4}

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.

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3
4    let topInset = view.safeAreaInsets.top
5    print("Top inset:", topInset)
6}

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.

swift
1let statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
2let navBarHeight = navigationController?.navigationBar.frame.height ?? 0
3let totalTopHeight = statusBarHeight + navBarHeight
4
5print("Status bar:", statusBarHeight)
6print("Navigation bar:", navBarHeight)
7print("Combined top height:", totalTopHeight)

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.

swift
1override func viewDidLayoutSubviews() {
2    super.viewDidLayoutSubviews()
3
4    if let navBarHeight = navigationController?.navigationBar.frame.height {
5        print("Navigation bar height:", navBarHeight)
6    }
7}

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.

swift
1if let navBar = navigationController?.navigationBar {
2    print("Prefers large titles:", navBar.prefersLargeTitles)
3    print("Current height:", navBar.frame.height)
4}

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.

swift
1customView.translatesAutoresizingMaskIntoConstraints = false
2
3NSLayoutConstraint.activate([
4    customView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
5    customView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
6    customView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
7])

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.height when you need the navigation bar's current height.
  • Use view.safeAreaInsets.top when 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.