为什么 additionalSafeAreaInsets 会传播到 child 个 VC?

Why is additionalSafeAreaInsets propagated to child VCs?

根据 Apple 的文档,没有这方面的证据,但我发现 additionalSafeAreaInsets 传播到 childVCs,但显然是这样。

运行 this gist 在 iPhone 7 模拟器上将输出容器 VC 和 child VC 都有一个底部88 的安全区域插图,当我期望只有 Container VC 有任何,而 ChildVC 会有 0(并且在 iPhone X 和更新版本上,只有数量Home Indicator 需要)。

有什么方法可以实现这种行为吗?

谢谢!

这不是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.

顺便说一句,这不是传播。这是一件更聪明的事情,很有意义。如果您将 ContainerViewController 的安全区域设置为底部的 80 points,并且您的 ChildViewController 与该区域的 X 点重叠,UIKit 会自动设置安全区域此子视图控制器的区域为 X 点,因此 none 子视图可能会离开您的安全区域。

例如,如果您设置 childVC 那样的约束条件

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)
])

在日志中您会看到,ChildViewController 安全区域高度为 50。这正是重叠的高度。