iPhone X - Hidden status bar pushes Navigation Bar upwards

Roi Mulia picture Roi Mulia · Sep 29, 2017 · Viewed 12.6k times · Source

I'm testing iPhone X behavior using the Xcode simulator. For some odd reason, if I'm hiding the Status Bar the Navigation Bar pushed upwards causing the title to completely disappear and cutting the left and right buttons. This is happening only on the iPhone X.

Illustration:

enter image description here

How can I hide the status bar and keep the Navigation Bar at a visible position?

UPDATE:

Sample project for your convince:

https://drive.google.com/file/d/0B5qJARV-Oc9ra1hvZkpXZm9lRUE/view?usp=sharing

Answer

nathangitter picture nathangitter · Sep 29, 2017

One solution is to embed the navigation controller inside a container view controller which is properly constrained to the safe area.

storyboard setup

Just create another view controller and drag a "Container View" from the Object Library. This view has top, bottom, leading, and trailing constraints to the safe area, all with constants equal to 0. If you control-drag from the container view to the navigation controller, you'll get an option to set an "embed segue" which will embed the selected view controller as a child view controller.

I set the status bar to be hidden on the new view controller I created, and it works fine.

screenshot of working layout on iPhone X

This feels like something that UINavigationBar should handle automatically, but this workaround seems like it should work pretty well.

Another note: if you change the color of the navigation bar, you'll also need to create another view above the container view, and change its color to match the color of the navigation bar.