iOS Development
Status Bar Height
Programming
Swift
iOS UI Elements

How to programmatically get iOS status bar height

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting the iOS status bar height used to be a direct UIApplication question, but modern iOS layout is more scene-based and safe-area-based. In many cases, the value you actually want is not the raw status bar height at all, but the top safe-area inset for the current view.

Use the Scene's Status Bar Manager When You Need the Raw Height

If you truly need the status bar frame height, read 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 is the modern replacement for older UIApplication.shared.statusBarFrame access.

The timing matters. If the view is not attached to a window yet, there may not be a usable windowScene, which is why viewDidLayoutSubviews or a similarly late lifecycle point is often safer than early initialization.

For Layout, Prefer safeAreaInsets.top

Most of the time, using the raw status bar height is the wrong design goal. If you are trying to position UI under the system bars correctly, safeAreaInsets.top is usually what you really 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}

This value accounts for the status bar and other top-area system constraints more reliably across devices such as those with a notch or dynamic top area behavior.

In other words, the safe area answers the layout question, while the status bar frame answers a narrower measurement question.

Older APIs and Migration Context

In older iOS versions, developers often used this pattern:

swift
let oldHeight = UIApplication.shared.statusBarFrame.height

That approach is now outdated for scene-based apps and is not the direction modern UIKit layout expects. If you are maintaining older code, moving toward scene-aware access or safe-area layout usually improves correctness immediately.

Dynamic Changes Still Matter

Status-bar-related top space can change during runtime. Phone calls, hotspot usage, navigation prompts, and other system overlays have historically affected the visible top area in different iOS configurations.

That is another reason to avoid hard-coding a fixed number such as 20 or 44. Query the current environment and let Auto Layout adapt when possible.

A Practical Constraint Example

If you do need to update a top constraint manually, use the safe area or scene height inside a layout-aware 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}

This is safer than baking in device-specific constants.

In well-structured UIKit layouts, Auto Layout and safe-area anchors often remove the need to read this value manually at all. If you can express the layout with constraints instead of measurement code, that is usually the more future-proof design.

Common Pitfalls

Reading the status bar height too early can return the wrong value because the view is not yet attached to a window scene.

Using raw status bar height for layout when safe-area insets would answer the real problem usually makes the code more brittle.

Hard-coding top offsets for specific devices is fragile and unnecessary in modern iOS UI code.

Summary

  • Use windowScene?.statusBarManager?.statusBarFrame.height if you truly need the raw status bar height.
  • Use view.safeAreaInsets.top for most layout decisions.
  • Query the value at a lifecycle point where the view is already attached to a window.
  • Avoid hard-coded constants for status-bar-related spacing.

Course illustration
Course illustration

All Rights Reserved.