我的应用程序包含一个主容器视图,其中交换了三个不同的视图(subviewA,subviewB和subviewC)。 subviewA和subviewB都要使用窗口调整大小,我按照以下步骤进行操作
NSView *myCurrentView = [_myCurrentViewController view];
if ([[_myCurrentViewController title] isEqualToString:subviewA] ||
[[_myCurrentViewController title] isEqualToString:subViewB]) {
[_myScrollView setHidden:YES];
[myCurrentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_mainWindowView addSubview: [_myCurrentViewController view]];
[_mainWindowView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
[_mainWindowView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
}
这样可以正常工作,但是对于subviewC,我想拥有子视图的水平尺寸,使用窗口调整大小,但使用滚动显示垂直。所以,在IB中我添加了一个NSScrollView作为我的mainWindowView的子视图。如果选择了subViewA或subviewB,我会隐藏滚动视图,但我不知道如何正确配置subviewC的约束。我想我可以添加水平约束类似于subviewA或subviewB,因为我喜欢视图用窗口调整大小,但是我不知道在垂直方向添加什么约束以允许滚动,到目前为止我有这个
[[_myCurrentViewController view] setTranslatesAutoresizingMaskIntoConstraints:YES];
[_myScrollView setHidden:NO];
[_myScrollView setDocumentView:[_myCurrentViewController view]];
[_myScrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
由于