Search code examples
iosuikitsafearealayoutguide

Why is additionalSafeAreaInsets propagated to child VCs?


There is no evidence of this according to Apple's documentation, but I have found that additionalSafeAreaInsets is propagated to childVCs, but it's clearly the case.

Running this gist on an iPhone 7 simulator will output that both the container VC and the child VC have a bottom safe area inset of 88, when I would expect that only the Container VC to have any, and instead the ChildVC would have 0 (and on iPhone X and newer, just the amount needed for the Home Indicator).

Is there any way of getting this behaviour?

Thanks!


Solution

  • Isn't that the evidence from Apple Documentation?

    UIKit container view controllers already adjust the safe area of their child view controllers to account for content views. For example, navigation controllers extend the safe area of their child view controllers to account for the navigation bar.

    By the way, it's not a propagation. It's a more smart thing that makes a lot of sense. If you set for your ContainerViewController its safe area to 80 points from the bottom, and your ChildViewController overlaps X points of the area, UIKit automaticall set the safe area for this child view controller to X points, so none subviews could get out of your safe area.

    For example, if you'll set you childVC constraints like that

    NSLayoutConstraint.activate([
        childVC.view.heightAnchor.constraint(equalToConstant: childVCHeight),
        childVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        childVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        // move childVC 30 points to the top
        childVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30)
    ])
    

    In the log you'll see, that ChildViewController safe area height is 50. This is exactly the height of overlap.