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:
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:
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:
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:
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.
Related reading
- How to programmatically send SMS on the iPhone?
- How to programmatically set drawableLeft on Android button?
- How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
- How to programmatically take a screenshot on Android?
- How to provide a localized description with an Error type in Swift?
- How to put a border around an Android TextView?
- How to quit android application programmatically
- How to quit or close single simulator from opened multiple simulator in Xcode 9?
.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 courseNo 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 courseData 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.