A status bar appears along the upper edge of the screen and displays information about the device's current state, like the time, cellular carrier, and battery level.
[UIApplication sharedApplication].statusBarFrame.size.height
. But since all sizes are in points, not in pixels, status bar height always equals 20.
Update. Seeing this answer being considered helpful, I should elaborate.
Status bar height is, indeed, equals 20.0f points except following cases:
setStatusBarHidden:withAnimation:
method and its height equals 0.0f points;There's also a case of status bar affecting the height of your view. Normally, the view's height equals screen dimension for given orientation minus status bar height. However, if you animate status bar (show or hide it) after the view was shown, status bar will change its frame, but the view will not, you'll have to manually resize the view after status bar animation (or during animation since status bar height sets to final value at the start of animation).
Update 2. There's also a case of user interface orientation. Status bar does not respect the orientation value, thus status bar height value for portrait mode is [UIApplication sharedApplication].statusBarFrame.size.height
(yes, default orientation is always portrait, no matter what your app info.plist says), for landscape - [UIApplication sharedApplication].statusBarFrame.size.width
. To determine UI's current orientation when outside of UIViewController
and self.interfaceOrientation
is not available, use [UIApplication sharedApplication].statusBarOrientation
.
Update for iOS7. Even though status bar visual style changed, it's still there, its frame still behaves the same. The only interesting find about status bar I got – I share: your UINavigationBar
's tiled background will also be tiled to status bar, so you can achieve some interesting design effects or just color your status bar. This, too, won't affect status bar height in any way.
Go with Martin's suggestion to the question: Get iPhone Status Bar Height.
CGFloat AACStatusBarHeight()
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
And in Swift
func statusBarHeight() -> CGFloat {
let statusBarSize = UIApplication.shared.statusBarFrame.size
return Swift.min(statusBarSize.width, statusBarSize.height)
}
It seems like a hack, but it's actually pretty solid. Anyway, it's the only working solution.
The following code, which would go in your custom subclass of UIViewController
, almost worked to support landscape. But, I noticed a corner case (when rotating from right > unsupported upside-down > left) for which it didn't work (switched height & width).
BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait;
CGSize statusBarSize = [UIApplication sharedApplication].statusBarFrame.size;
CGFloat statusBarHeight = (isPortrait ? statusBarSize.height : statusBarSize.width);
Try this:
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
Swift 3 or Swift 4:
UIApplication.shared.statusBarFrame.height
While the status bar is usually 20pt tall, it can be twice that amount in some situations:
Just try it, and you'll see for yourself. Hardcoding the height to 20pt will usually work, until it doesn't.
So I second H2CO3's answer:
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
EDIT
The iOS 11 way to work out where to put the top of your view content is UIView's safeAreaLayoutGuide
See UIView Documentation.
DEPRECATED ANSWER
If you're targeting iOS 7+, The documentation for UIViewController advises that the viewController's topLayoutGuide
property gives you the bottom of the status bar, or the bottom of the navigation bar, if it's also visible. That may be of use, and is certainly less hack than many of the previous solutions.
For iOS 13 you can use:
UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame.height
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With