如何以编程方式获取iOS状态栏高度 [英] How to programmatically get iOS status bar height

查看:184
本文介绍了如何以编程方式获取iOS状态栏高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道目前iPhone / iPad顶部的状态栏(时间,电池和网络连接)对于非视网膜屏幕为20像素,对于视网膜屏幕为40像素,但未来我的应用程序证明我希望能够在没有硬编码值的情况下确定这一点。是否可以通过编程方式确定状态栏的高度?

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?

推荐答案

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

这篇关于如何以编程方式获取iOS状态栏高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆