Search code examples
iosswiftautolayoutscrollviewsubview

Last view added to content view of scrollview goes behind the home indicator


I have a UIViewController containing a scrollView.

The scrollView is pinned (0,0,0,0) to the Safe Area, this way:

enter image description here enter image description here

In the body of the viewController viewDidLayoutSubviews, I add some views to the Content View of this scrollView.

I see no issue in iPhone 6 simulator, but in iPhone X simulator something strange happens: the last view I add to the scrollview goes partially behind the home indicator:

enter image description here

I erroneously thought this was the kind of issue solved by the Safe Area.

What could I do to solve this issue?


Solution

  • As of iOS 11 and new iPhone X devices and forward this is the default behavior of safeAreaLayoutGuide.

    Scroll view subclasses like table view and collection view handles this problem automatically if you pin the bottom anchor to safe area.

    Since you are populating a scroll view manually you need to get the bottom safe area height and inset the scroll view with that value.

    You can do it like this:

    scrollView.contentInset.bottom = view.safeAreaInsets.bottom