且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式获取iOS状态栏高度

更新时间:2023-02-12 19:11:18

[UIApplication sharedApplication]。 statusBarFrame.size.height 。但由于所有尺寸均为分数,而不是像素,因此状态栏高度始终等于20.

[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.

状态栏高度确实等于20.0f点除了以下情况:

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


  • 状态栏已隐藏 setStatusBarHidden:withAnimation:方法及其高度等于0.0f点;

  • 正如@Anton在这里指出的那样,在电话应用程序之外的来电期间或录音期间状态栏高度等于40.0f点。

  • status bar has been hidden with setStatusBarHidden:withAnimation: method and its height equals 0.0f points;
  • 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.

还有一个状态栏会影响视图的高度。通常,视图的高度等于给定方向的屏幕尺寸减去状态栏高度。但是,如果在显示视图后为状态栏设置动画(显示或隐藏),状态栏将更改其框架,但视图将不会,您必须在状态后手动调整视图大小条形动画(或动画期间,因为状态栏高度在动画开始时设置为最终值)。

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).

更新2。还有一个案例用户界面方向。状态栏不考虑方向值,因此纵向模式的状态栏高度值为 [UIApplication sharedApplication] .statusBarFrame.size.height (是,默认方向始终为纵向,无论您的app.plist说明了什么), landscape - [UIApplication sharedApplication] .statusBarFrame.size.width 。要在 UIViewController self.interfaceOrientation 之外无法确定UI的当前方向,请使用 [UIApplication sharedApplication] .statusBarOrientation

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.

iOS7更新。即使状态栏视觉样式发生了变化,它仍然是在那里,它的框架仍然表现相同。关于我获得的状态栏的唯一有趣的发现 - 我分享:你的 UINavigationBar 平铺背景也将平铺到状态栏,所以你可以实现一些有趣的设计效果或只是为状态栏着色。这也不会以任何方式影响状态栏高度。

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.