iOS Development
Status Bar Height
Programming
Swift
iOS UI Elements

How to get the iOS status bar height programmatically (statusBarManager)

Codemia Knowledge Hub ·

On modern iOS, read the height from the window scene: view.window?.windowScene?.statusBarManager?.statusBarFrame.height. For layout, though, you usually want view.safeAreaInsets.top rather than the raw status bar height, because iOS layout is now scene-based and safe-area-based.

Read the raw height from the scene's status bar manager

If you really need the status bar frame height, get it from the active window scene:

swift
1import UIKit
2
3final class DemoViewController: UIViewController {
4    override func viewDidLayoutSubviews() {
5        super.viewDidLayoutSubviews()
6
7        let statusBarHeight = view.window?
8            .windowScene?
9            .statusBarManager?
10            .statusBarFrame.height ?? 0
11
12        print("status bar height:", statusBarHeight)
13    }
14}

This replaces the older UIApplication.shared.statusBarFrame access.

Timing matters. Until the view is attached to a window, there may be no usable windowScene, so viewDidLayoutSubviews or a similarly late lifecycle point is safer than early initialization.

For layout, use safeAreaInsets.top

If you are trying to place UI below the system bars, the top safe-area inset is usually the value you need:

swift
1import UIKit
2
3final class HeaderViewController: UIViewController {
4    override func viewDidLayoutSubviews() {
5        super.viewDidLayoutSubviews()
6
7        let topInset = view.safeAreaInsets.top
8        print("top safe area:", topInset)
9    }
10}

It accounts for the status bar and other top-area system constraints more reliably across devices, including those with a notch or a dynamic top area. The safe area answers the layout question; the status bar frame answers a narrower measurement question.

The old UIApplication.shared.statusBarFrame API

Older code often used:

swift
let oldHeight = UIApplication.shared.statusBarFrame.height

That approach is outdated for scene-based apps. When maintaining older code, switching to scene-aware access or safe-area layout usually improves correctness right away.

Why you should not hard-code 20 or 44

The top space can change at runtime. Phone calls, hotspot usage, navigation prompts and other system overlays have historically changed the visible top area in various iOS configurations. Query the current environment instead of using a fixed number such as 20 or 44, and let Auto Layout adapt where possible.

Updating a top constraint manually

If you must set a top constraint in code, use the safe area inside a layout callback:

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    @IBOutlet private weak var topConstraint: NSLayoutConstraint!
5
6    override func viewDidLayoutSubviews() {
7        super.viewDidLayoutSubviews()
8        topConstraint.constant = view.safeAreaInsets.top
9    }
10}

In well-structured UIKit layouts, Auto Layout and safe-area anchors often remove the need to read this value at all. Expressing the layout with constraints is usually more future-proof than measurement code.

Mistakes to avoid

  • Reading the height too early, before the view is attached to a window scene, which can return the wrong value.
  • Using the raw status bar height for layout when safe-area insets answer the real problem.
  • Hard-coding top offsets for specific devices.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms