驻留在单独的 xib 中的自动布局子视图

Autolayout subviews residing in separate xib

除了标题所说的

  1. Main window 左侧有一个容器视图,称之为 mainContainerView(绿色背景)
  2. 在第二个 .xib 中我有一个 NSTableView 设置 - 称之为 Second.xib(红色背景)
  3. 在MainViewController.m中我添加了在Second.xib中定义的NSTableView 并将约束设置如下

[_mainContainerView addConstraint:[NSLayoutConstraint constraintWithItem:_mainContainerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.second.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];

正如您在屏幕截图中看到的,当我调整 window 大小时,红色背景没有跟随主容器视图。你知道为什么会这样吗?

编辑 + 解决

为了修复这个错误,我必须为 width/height 设置约束。我还添加了 top/bottom 与 superview 对齐的约束。

我更愿意:

[_mainContainerView addConstraint:[NSLayoutConstraint constraintWithItem:self.second.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_mainContainerView attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f]];