objective-c - Autolayout NSScrollview 不增长 contentView

标签 objective-c cocoa autolayout

我有一个 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/

相关文章:

ios - 如何在导航 Controller 中将数据从一个 View 传递到另一个 View

Objective-C NSWindow 从键中删除窗口

c++ - 从基于 C++/QT 的应用程序访问 NSWindow

ios - 自动布局 swift 在标签上无法正常工作

ios - 在 iOS 中的文件夹 OneDrive 中创建文件夹

ios - 从 segue 返回后,UIButton 中心移回默认位置

macos - 未找到 CoreImage.framework

ios - 在 Xcode 8 中工作时 Storyboard变得困惑

ios - 如何使用自动布局以编程方式快速设置旋转时 UICollectionView 的宽度

objective-c - 为什么分配时需要临时变量?