我有一个 ScrollView ,我正在尝试使用自动布局进行设置。
实际的内容是在一个容器 View 中,这个容器 View 是一组可见的盒 subview 。它们可以毫无问题地添加到容器 View 中。每个人都受到一些限制
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]];
但是, ScrollView 不允许容器 View 在不增长 ScrollView 本身的情况下增长。 containerView
的右边缘附加到 ScrollView 的右边缘。如果我增大 ScrollView ,我可以看到列表容器 View 中有更多内容,但仅与 ScrollView 一样大。
最后一行是我尝试通过基本上说“让容器 View 变得比 ScrollView 更大”来解决这个问题。这不是设置约束的正确方法吗?
最佳答案
将 NSStackView
添加到您的 NIB,然后选择编辑器 > 嵌入 > ScrollView 。向 ScrollView 添加约束,以保持其相对于其 super View 和/或同级 View 的布局。在堆栈 View 及其 super View (剪辑 View )之间添加约束,以保持其顶部和前缘与剪辑 View 的相同。添加约束以保持堆栈 View 的后缘大于或等于剪辑 View 的后缘。添加约束以保持堆栈 View 的底部边缘大于或等于剪辑 View 的底部边缘。
在您的代码中,将 vcs
中的 View Controller 的 View 添加为堆栈 View 的 subview 。堆栈 View 的间距默认为 8。在代码中,您将间距设置为 5。因此,您可以将堆栈 View 的间距更改为 5。(这可以在 NIB 中完成。)
关于objective-c - Autolayout NSScrollview 不增长 contentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29155211/