使用 intrinsicContentsize 嵌入 UIViewController

Embedding UIViewController with intrinsicContentsize

我尝试实现一个视图,该视图在故事板中的顶部有一个具有动态高度的视图,在 UICollectionView 下方。

只要我直接将其布局在一个 ViewController 中,一切正常。

当顶视图由一个单独的ViewController管理并嵌入时,我总是运行陷入autolayout问题:

SizingSample[53440:11697282] [LayoutConstraints] Unable to simultaneously satisfy constraints. (
    "<NSLayoutConstraint:0x6000030cc690 UILayoutGuide:0x600002af9180'UIViewSafeAreaLayoutGuide'.bottom == SizingSample.DynamicSizeView:0x7fda024197b0.bottom + 3   (active)>",
    "<NSLayoutConstraint:0x6000030cc960 SizingSample.DynamicSizeView:0x7fda024197b0.top == UILayoutGuide:0x600002af9180'UIViewSafeAreaLayoutGuide'.top + 20   (active)>",
    "<NSLayoutConstraint:0x6000030c52c0 'UIView-Encapsulated-Layout-Height' UIView:0x7fda024195d0.height == 0   (active)>",
    "<NSLayoutConstraint:0x6000030cc870 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x600002af9180'UIViewSafeAreaLayoutGuide']-(0)-|   (active, names: '|':UIView:0x7fda024195d0 )>",
    "<NSLayoutConstraint:0x6000030cc7d0 'UIViewSafeAreaLayoutGuide-top' V:|-(0)-[UILayoutGuide:0x600002af9180'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7fda024195d0 )>"
)

IMO 'UIView-Encapsulated-Layout-Height' 约束破坏了一切,但我还没有找到任何方法来阻止它。

可在 https://github.com/tengelmeier/viewcontroller-embedding-problem.git

找到条纹羽绒服样品

如何根据嵌入式视图(..控制器)的动态大小自动布局视图?

因为ios嵌入时自动应用固定框架集ContainerController.you不得不告诉不要将自动调整大小的掩码转换为LayoutConstraint。

您可以通过在 ContainerViewController(目标控制器)ViewdidLoad 方法中设置以下 属性 来完成此操作。

view.translatesAutoresizingMaskIntoConstraints = false