Safe area not work properly on iPhone X when subviews are not in the view area Safe area not work properly on iPhone X when subviews are not in the view area xcode xcode

Safe area not work properly on iPhone X when subviews are not in the view area


instead of referencing the current view's safeAreaInsets, set it to the UIApplication:

(UIApplication.shared.delegate?.window??.safeAreaInsets.bottom)


In your child view controllers if you set the view controllers additionalSafeAreaInsets equal to the window's safe area insets they will layout correctly respecting the safe areas.

I found I had to do this inside of viewDidLoad() and viewWillTransition(to size: CGSize, with coordinator: UIVIewControllerTransitionCoordinator

Inside of viewWillTransition you will want to set the additionalSafeAreaInsets in the animation block of the coordinator:

coordinator.animate(alongsideTransition: { _ in    if #available(iOS 11.0, *) {        self.additionalSafeAreaInsets = UIApplication.shared.delegate?.window??.safeAreaInsets    }}, completion: nil)


I was building a custom paging view controller and ran into this issue as well @PowHU.

The only solution that seemed to work for me was to set the view controller's view class in the storyboard to a custom class I created called AlwaysSafeAreaInsetsView.

import UIKitclass AlwaysSafeAreaInsetsView: UIView {    @available(iOS 11.0, *)    override var safeAreaInsets: UIEdgeInsets {        if let window = UIApplication.shared.keyWindow {            return window.safeAreaInsets        }        return super.safeAreaInsets    }}