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