如何通過編程方式獲得iOS狀態欄高度

[英]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 个解决方案

#1


473  

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

事實上,狀態欄的高度等於20.0f點,除了以下情況:

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

iOS7更新。雖然狀態欄的視覺樣式改變了,但它仍然在那里,它的框架仍然是一樣的。關於狀態欄我得到的唯一有趣的發現——我分享:你的UINavigationBar的平鋪背景也將被平鋪到狀態欄,這樣你就可以實現一些有趣的設計效果,或者只是給你的狀態欄上色。這也不會以任何方式影響狀態欄的高度。

Navigation bar tiled background is also tiled to status bar

#2


86  

Go with Martin's suggestion to the question: Get iPhone Status Bar Height.

按照馬丁的建議去問這個問題:獲得iPhone狀態欄的高度。

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

下面的代碼將在UIViewController的自定義子類中運行,幾乎可以支持風景。但是,我注意到一個角落的情況(當從右>旋轉時,不支持上下顛倒的>向左),它不能工作(切換高度和寬度)。

BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait;
CGSize statusBarSize = [UIApplication sharedApplication].statusBarFrame.size;
CGFloat statusBarHeight = (isPortrait ? statusBarSize.height : statusBarSize.width);

#3


20  

Try this:

試試這個:

CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;

#4


10  

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

雖然狀態欄通常是20pt高,但在某些情況下,它可能是這個數字的兩倍:

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

只要試一試,你就會自己看到的。將高度硬編碼為20pt通常是有效的,直到它失效。

So I second H2CO3's answer:

第二個是H2CO3的答案

statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;

#5


9  

Swift 3 or Swift 4:

Swift 3或Swift 4:

UIApplication.shared.statusBarFrame.height

#6


7  

EDIT The iOS 11 way to work out where to put the top of your view content is UIView's safeAreaLayoutGuide See UIView Documentation.

編輯ios11的方法,找出在哪里放置你的視圖內容的頂部是UIView的safeAreaLayoutGuide見UIView文檔。

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屬性會給你狀態欄的底部,或者導航欄的底部,如果它也是可見的。這可能是有用的,而且肯定比以前的許多解決方案要簡單。

#7


5  

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.

然后您可以直接讀取statusBarFrame的高度屬性。這個實例中的“View”應該是您希望使用度量的視圖,很可能是應用程序窗口的根視圖控制器。

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.

順便說一句,在打電話時,狀態欄不僅可能更高,而且如果故意隱藏狀態欄,它也可能為零。

#8


2  

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

這些是我的一個項目中的標准函數:https://github.com/goktugyil/ezswiftextense

#9


0  

By default Status Bar height in iOS is 20 pt.

iOS默認狀態欄高度為20pt。

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

更多信息:http://www.idev101.com/code/User_Interface/sizes.html。

#10


0  

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長度=狀態條高度

Swift:

迅速:

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。因此,通過使用這種方法,您可以輕松地計算狀態欄高度,而不必擔心由於電話而導致的狀態欄高度變化。

#11


0  

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:

下面的解決方案對所有現代iOS版本都非常有用:

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

or just

或者只是

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

#12


-6  

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

使用下面的一行代碼,您可以在任何方向上獲得狀態欄高度,如果它是可見的,也可以不可見

#define STATUS_BAR_HIGHT (
    [UIApplicationsharedApplication].statusBarHidden ? 0 : (
        [UIApplicationsharedApplication].statusBarFrame.size.height > 100 ?
            [UIApplicationsharedApplication].statusBarFrame.size.width :
            [UIApplicationsharedApplication].statusBarFrame.size.height
    )
)

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

它只是一個簡單但非常有用的宏,您不需要編寫任何額外的代碼

关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/20/7200e06add9dfbe1f9f25e278d6fe5ae.html



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