Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollectionView header content under status bar

Here's a collection view constrained to top, left, right, and bottom of the superview with safe area layout guide enabled:

enter image description here

I want my collection view header to be shown under the status bar. I've achieved this for iPhone 4 - 8+ screen dimensions by unchecking Safe Area Layout Guide in the size inspector for the controller's main view, and adding the following code:

collectionView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)

This looks great for non iPhone X view sizes:

enter image description here

However, for the iPhone X, this leads to the following output:

enter image description here

The iPhone X has its own dimensions for the status bar. Adjusting the top inset further does work, but will over-offset the other device sizes. I am wondering if there's an more elegant way to achieve this behaviour.

like image 773
Kelvin Lau Avatar asked Feb 22 '18 19:02

Kelvin Lau


1 Answers

Found a solution:

collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height
like image 180
Kelvin Lau Avatar answered Sep 21 '22 06:09

Kelvin Lau