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

查看:17
本文介绍了如何以编程方式获取 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. 还有一个用户界面方向的案例.状态栏不考虑方向值,因此 portrait 模式的状态栏高度值是 [UIApplication sharedApplication].statusBarFrame.size.height (是的,默认方向始终是纵向,无论您的应用程序 info.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 更新. 尽管状态栏视觉样式发生了变化,但它仍然存在,它的框架仍然表现相同.我得到的关于状态栏的唯一有趣的发现——我分享一下:你的 UINavigationBartiled 背景也会被平铺到状态栏,这样你就可以实现一些有趣的设计效果或只需为您的状态栏着色.这也不会以任何方式影响状态栏的高度.

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天全站免登陆