2

To get the height of the statusbar of the iOS application, we do

let height = UIApplication.shared.statusBarFrame.height
print(height)

and this returns 20.0 in a normal situation.

If the statusbar has expanded due to in-call, or personal hotspot, condition, this will return 40.0.

Now let us say that we have a NavigationController with a UICollectionView built-in. And we're trying to figure out the location where the contentOffset of the collection view points to.

let contentOffset = self.collectionView!.contentOffset
let statusBarHeight = UIApplication.shared.statusBarFrame.height

With a normal statusbar, the results are:

contentOffset: (0.0, -64.0)

statusBarHeight: 20.0

Meaning that the NavigationBar's height is 64.0.

And with expanded statusbar:

contentOffset: (0.0, -64.0)

statusBarHeight: 40.0

Notice the contentOffset is still (0.0, -64.0) but not (0.0, -84.0). This means that there's unreachable space behind the expanded statusbar. And I believe there's no way we can figure out the actual size of this unreachable space.

We can only guess it is 20.0 since we already know this number in practice.

enter image description here

enter image description here

So let say we want to draw a nice view right below the statusbar. But using the information given by statusBarHeight, we can't do that since the whole view will shrink it's height by magical 20px that we don't know about.

If we want to place a view of frame (0, statusBarHeight, 100, 100), this will be placed right below the status bar if the status bar is in the normal state. But if the status bar has expanded, this will fail because now the statusBarHeight is 40.

Now we have to add the magic number like below:

myView.frame = CGRect(x: 0, y: statusBarHeight - 20, width: 100, height: 100)

So my question is:

How do we compute this magic number?

sCha
  • 1,454
  • 1
  • 12
  • 22

1 Answers1

2

Your app delegate can implement these methods:

  • -application:willChangeStatusBarFrame:
  • -application:didChangeStatusBarFrame:

and these local notifications will also be sent to the default notification center:

  • UIApplicationWillChangeStatusBarFrameNotification
  • UIApplicationDidChangeStatusBarFrameNotification

Here you go to statusBarHeight for compute magic number

More check this Get notification status Bar Height change

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • Yes these methods will give you information about `when` does this change happens, and the height will be `40.0` if expanded. But I want to know how much the entire view did shrink in it's height which is not 40.0 it's 20.0. – sCha Sep 20 '17 at 08:53
  • did you test anything that goes status bar 20 but not printed. for example recording or anything else . more check this one https://stackoverflow.com/questions/3888517/get-iphone-status-bar-height?rq=1 – Nazmul Hasan Sep 20 '17 at 09:05