[英]How to programmatically get iOS status bar height

I know that currently the status bar (with the time, battery, and network connection) at the top of the iPhone/iPad is 20 pixels for non-retina screens and 40 pixels for retina screens, but to future proof my app I would like to be able to determine this without hard coding values. Is it possible to figure out the height of the status bar programmatically?

我知道目前状态栏(时间,电池,和网络连接)顶部non-retina iPhone / iPad是20像素的屏幕和40像素视网膜屏幕,但未来证明我的程序我想能够确定这个没有硬编码值。是否可以通过编程方式计算状态栏的高度?

12 个解决方案



[UIApplication sharedApplication].statusBarFrame.size.height. But since all sizes are in points, not in pixels, status bar height always equals 20.

UIApplication sharedApplication .statusBarFrame.size.height。但是由于所有大小都是以点为单位,而不是以像素为单位,所以状态栏的高度总是等于20。

Update. Seeing this answer being considered helpful, I should elaborate.


Status bar height is, indeed, equals 20.0f points except following cases:


  • status bar has been hidden with setStatusBarHidden:withAnimation: method and its height equals 0.0f points;
  • 状态栏隐藏在setStatusBarHidden:withAnimation:方法及其高度= 0.0f点;
  • as @Anton here pointed out, during an incoming call outside of Phone application or during sound recording session status bar height equals 40.0f points.
  • 正如@Anton指出的那样,在电话应用程序之外的来电或录音会话期间,条形高等于40.0f点。

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.

更新2。还有一个用户界面朝向的例子。状态栏不尊重方向值,因此竖屏模式的状态栏高度值为[UIApplication sharedApplication]. statusbarframe .size。高度(是的,不管你的应用信息是什么,默认的方向总是竖的。plist说),用于横向- [UIApplication sharedApplication]. statusbarframe . sizy .width。要确定UI在UIViewController和self。interfaceorientation之外不可用时的当前方向,请使用[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.


Navigation bar tiled background is also tiled to status bar



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.


Old Answer

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;



While the status bar is usually 20pt tall, it can be twice that amount in some situations:


  • when you're in the middle of a phone call (that's a pretty common scenario);
  • 当你在打电话的时候(这是很常见的情况);
  • when the voice recorder, or Skype, or a similar app, is using the microphone in the background;
  • 当语音记录器,或Skype,或类似的应用程序,在后台使用麦克风;
  • when Personal Hotspot is activated;
  • 当个人热点被激活时;

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;



Swift 3 or Swift 4:

Swift 3或Swift 4:




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.

不赞成的回答如果你针对的是ios7 +, UIViewController的文档告诉你viewController的topLayoutGuide属性会给你状态栏的底部,或者导航栏的底部,如果它也是可见的。这可能是有用的,而且肯定比以前的许多解决方案要简单。



Don't forget that the status bar's frame will be in the screen's coordinate space! If you launch in landscape mode, you may find that width and height are swapped. I strongly recommend that you use this version of the code instead if you support landscape orientations:


CGRect statusBarFrame = [self.window convertRect:[UIApplication sharedApplication].statusBarFrame toView:view];

You can then read statusBarFrame's height property directly. 'View' in this instance should be the view in which you wish to make use of the measurements, most likely the application window's root view controller.


Incidentally, not only may the status bar be taller during phone calls, it can also be zero if the status bar has been deliberately hidden.




Here is a Swift way to get screen status bar height:


var screenStatusBarHeight: CGFloat {
    return UIApplication.sharedApplication().statusBarFrame.height

These are included as a standard function in a project of mine: https://github.com/goktugyil/EZSwiftExtensions




By default Status Bar height in iOS is 20 pt.


More info: http://www.idev101.com/code/User_Interface/sizes.html




I just found a way that allow you not directly access the status bar height, but calculate it.


Navigation Bar height - topLayoutGuide length = status bar height

导航条高度- topLayoutGuide长度=状态条高度



let statusBarHeight = self.topLayoutGuide.length-self.navigationController?.navigationBar.frame.height

self.topLayoutGuide.length is the top area that's covered by the translucent bar, and self.navigationController?.navigationBar.frame.height is the translucent bar excluding status bar, which is usually 44pt. So by using this method you can easily calculate the status bar height without worring about status bar height change due to phone calls.

self.topLayoutGuide。长度是半透明条覆盖的顶部区域,self。navigationcontroller ?. navigationbar .frame.height是不含状态条的半透明条,通常为44pt。因此,通过使用这种方法,您可以轻松地计算状态栏高度,而不必担心由于电话而导致的状态栏高度变化。



To improve the other answers:


lazy is better here.


private lazy var statusBarHeight: CGFloat = {
        let statusBarSize = UIApplication.shared.statusBarFrame.size
        return min(statusBarSize.width, statusBarSize.height)

But the problems about minimum values appear only for old (iOS 7) version.

但是关于最小值的问题只出现在老版本(iOS 7)中。

The solution below is really useful for all modern iOS versions:


private lazy var statusBarHeight: CGFloat = {
            return UIApplication.shared.statusBarFrame.size.height

or just


private let statusBarHeight = UIApplication.shared.statusBarFrame.size.height



Using following single line code you can get status bar height in any orientation and also if it is visible or not


    [UIApplicationsharedApplication].statusBarHidden ? 0 : (
        [UIApplicationsharedApplication].statusBarFrame.size.height > 100 ?
            [UIApplicationsharedApplication].statusBarFrame.size.width :

It just a simple but very useful macro just try this you don't need to write any extra code





© 2014-2019 ITdaan.com 粤ICP备14056181号