Autolayout NSScrollview 不会增长 contentView

Autolayout NSScrollview does not grow contentView

我有一个滚动视图,我正在尝试使用自动布局进行设置。

实际内容在一个容器视图中,这个容器视图是一组可见的框视图。它们被毫无问题地添加到容器视图中。每个人都有几个约束条件

NSViewController prev;
for(NSViewController *lvc in vcs) {
//each view is padded by 5 from left
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:prev.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:5]];
//Constant height
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300]];
//Constant width
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:lvc.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300]];
prev = lvc;

}


[_scrollView setDocumentView:containerView];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:listContainerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:_scrollView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];

然而,滚动视图不允许容器视图在不增长滚动视图本身的情况下增长。 containerView 的右边缘附加到滚动视图的右边缘。如果我增大滚动视图,我可以看到列表容器视图中有更多内容,但仅与滚动视图一样大。

最后一行是我试图通过基本上说 "let the container view grow larger than the scroll view" 来解决这个问题。这不是设置约束的正确方法吗?

NSStackView 添加到您的 NIB,然后 select 编辑器 > 嵌入 > 滚动视图。向滚动视图添加约束以使其相对于其父视图 and/or 兄弟视图进行布局。在堆栈视图和它的父视图(剪辑视图)之间添加约束以保持其顶部和前导边缘与剪辑视图的相同。添加约束以保持堆栈视图的后缘大于或等于剪辑视图的后缘。添加约束以保持堆栈视图的底部边缘大于或等于剪辑视图的底部边缘。

在您的代码中,将 vcs 中的视图控制器的视图添加为堆栈视图的子视图。堆栈视图的间距默认为 8。在您的代码中,您将间距设置为 5。因此,您可以将堆栈视图的间距更改为 5。(这可以在 NIB 中完成。)