Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude navigation bar from iOS "safe area"

In my iOS app, I have views rendered beneath the navigation bar. This is because the navigation bar is hidden until the user taps the screen.

The screenshots below illustrate my problem.

The "X" button is rendered beneath the iPhone X's notch and can hardly be seen. This is when I constrain the button's topAnchor to its superview!.topAnchor.

Note that this works as intended for all devices except the iPhone X.

enter image description here

The "X" button in this screenshot is anchored to its superview!.safeAreaLayoutGuide.topAnchor and renders below the navigation bar. This makes sense given Apple's documentation on safeAreaLayoutGuide:

"this guide reflects the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor views."

enter image description here

However, I want the "X" button to render below the iPhone X's notch and underneath the navigation bar. Here's what it looks like when the navigation bar is hidden:

enter image description here

The "X" button should render right below the notch.

So my question is:

Is there a way to exclude the navigation bar from a view's safeAreaLayoutGuide? And if not, what are my options, other than manually offsetting the button on iPhone X's.

Note that I'm doing everything programmatically. I no longer use Storyboards.

Thanks!

like image 797
cohenadair Avatar asked May 16 '18 07:05

cohenadair


People also ask

How do I hide navigation bar on Iphone?

Go to Settings > Accessibility > Guided Access and toggle on Guided Access. Open an app and triple-click the Power button to enter Guided Access. There's no other way to get rid of the Home Bar.

How do I hide items in navigation?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

Can you change Iphone navigation bar?

Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.


1 Answers

You can change the view controllers safe area insets by adding additional insets. Create a UIEdgeInsetsMake() object where you subtract the navigation bar height from the top inset. Then add it to the View Controllers additionalSafeAreaInsets.

Declaration: var additionalSafeAreaInsets: UIEdgeInsets { get set }

like image 105
Daniel R. Avatar answered Sep 21 '22 14:09

Daniel R.