防止破坏可折叠 NSSplitView 的约束
Prevent breaking constraint on collapsable NSSplitView
我有一个包含 NSSearchField 的 NSSplitView。它看起来像这样:
我使用以下代码创建了 NSSearchField:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
searchField = [[NSSearchField alloc] init];
searchField.translatesAutoresizingMaskIntoConstraints = NO;
[leftView addSubview: searchField];
[searchField.leadingAnchor constraintEqualToAnchor: leftView.leadingAnchor
constant: 1.0f].active = YES;
[searchField.trailingAnchor constraintEqualToAnchor: leftView.trailingAnchor
constant: -1.0f].active = YES;
[searchField.topAnchor constraintEqualToAnchor: leftView.topAnchor
constant: 1.0f].active = YES;
[searchField.heightAnchor constraintEqualToConstant: 22.0f].active = YES;
}
这很好用,我可以毫无问题地调整拆分器的大小。
但是,如果我添加以下 NSSplitViewDelegate 方法(试图使左侧完全可折叠):
- (CGFloat)splitView:(NSSplitView *)splitView
constrainMinCoordinate:(CGFloat)proposedMinimumPosition
ofSubviewAt:(NSInteger)dividerIndex
{
if(0 == dividerIndex)
{
return 0;
}
return proposedMinimumPosition;
}
现在,如果我调整拆分器的大小以使左视图折叠,则会违反约束条件。
AnchorTest[57668:1656122] [Layout] Unable to simultaneously satisfy constraints:
(
"<NSAutoresizingMaskLayoutConstraint:0x604000084c40 h=--& v=--& NSView:0x60c000120b40.width == 0.5 (active)>",
"<NSLayoutConstraint:0x60c0000822b0 H:|-(1)-[NSSearchField:0x100412290] (active, names: '|':NSView:0x60c000120b40 )>",
"<NSLayoutConstraint:0x60c000085f50 NSSearchField:0x100412290.trailing == NSView:0x60c000120b40.trailing (active)>"
)
这是有道理的。显然搜索字段 leading/trailing 锚点不能偏移 -1,如果我删除偏移量,约束没有问题。
问题是我想要 left/right 偏移量。必须有一种方法可以通过 AutoLayout 执行此操作,但我无法弄清楚。有谁知道如何正确地做到这一点?
将分隔线拖到最后与折叠不同。 Return 来自 constrainMinCoordinate
的最小可用大小并实施 splitView:canCollapseSubview:
.
我有一个包含 NSSearchField 的 NSSplitView。它看起来像这样:
我使用以下代码创建了 NSSearchField:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
searchField = [[NSSearchField alloc] init];
searchField.translatesAutoresizingMaskIntoConstraints = NO;
[leftView addSubview: searchField];
[searchField.leadingAnchor constraintEqualToAnchor: leftView.leadingAnchor
constant: 1.0f].active = YES;
[searchField.trailingAnchor constraintEqualToAnchor: leftView.trailingAnchor
constant: -1.0f].active = YES;
[searchField.topAnchor constraintEqualToAnchor: leftView.topAnchor
constant: 1.0f].active = YES;
[searchField.heightAnchor constraintEqualToConstant: 22.0f].active = YES;
}
这很好用,我可以毫无问题地调整拆分器的大小。
但是,如果我添加以下 NSSplitViewDelegate 方法(试图使左侧完全可折叠):
- (CGFloat)splitView:(NSSplitView *)splitView
constrainMinCoordinate:(CGFloat)proposedMinimumPosition
ofSubviewAt:(NSInteger)dividerIndex
{
if(0 == dividerIndex)
{
return 0;
}
return proposedMinimumPosition;
}
现在,如果我调整拆分器的大小以使左视图折叠,则会违反约束条件。
AnchorTest[57668:1656122] [Layout] Unable to simultaneously satisfy constraints:
(
"<NSAutoresizingMaskLayoutConstraint:0x604000084c40 h=--& v=--& NSView:0x60c000120b40.width == 0.5 (active)>",
"<NSLayoutConstraint:0x60c0000822b0 H:|-(1)-[NSSearchField:0x100412290] (active, names: '|':NSView:0x60c000120b40 )>",
"<NSLayoutConstraint:0x60c000085f50 NSSearchField:0x100412290.trailing == NSView:0x60c000120b40.trailing (active)>"
)
这是有道理的。显然搜索字段 leading/trailing 锚点不能偏移 -1,如果我删除偏移量,约束没有问题。
问题是我想要 left/right 偏移量。必须有一种方法可以通过 AutoLayout 执行此操作,但我无法弄清楚。有谁知道如何正确地做到这一点?
将分隔线拖到最后与折叠不同。 Return 来自 constrainMinCoordinate
的最小可用大小并实施 splitView:canCollapseSubview:
.