如果您将 ac 的 AttributeCenterX 与其父视图的 AttributeCenterX、AttributeLeading 或 AttributeTrailing 相关联,您应该能够使用乘数和约束来表达您想要的约束。请记住,仅在创建约束时才评估常量,并且示例的常量不会随着 ac.superview 的宽度变化而更新。
如果您可以用文字表达您希望 ac 相对于其父视图的位置,我们可以建议一个约束。
编辑
这是一个带有 5 个 NSButton 的示例。它们本身和它们之间的空间扩展,使得空间的宽度为按钮的 30%,所有按钮的宽度相同,所有空间的宽度都相同。仅为间距创建 4 个不可见的 NSView 非常麻烦,尤其是考虑到您已经让它在自动布局之外工作。但如果你好奇:
// Assuming these NSViews and NSButtons exist,
//NSView* superview ;
//NSButton *buttonOne, *buttonTwo, *buttonThree, *buttonFour, *buttonFive ;
[superView removeConstraints:superView.constraints] ;
// Create empty NSViews to fill the space between the 5 buttons.
NSView* spaceOne = [NSView new] ;
NSView* spaceTwo = [NSView new] ;
NSView* spaceThree = [NSView new] ;
NSView* spaceFour = [NSView new] ;
spaceOne.translatesAutoresizingMaskIntoConstraints = NO ;
spaceTwo.translatesAutoresizingMaskIntoConstraints = NO ;
spaceThree.translatesAutoresizingMaskIntoConstraints = NO ;
spaceFour.translatesAutoresizingMaskIntoConstraints = NO ;
[superView addSubview:spaceOne] ;
[superView addSubview:spaceTwo] ;
[superView addSubview:spaceThree] ;
[superView addSubview:spaceFour] ;
NSDictionary* views = NSDictionaryOfVariableBindings(superView,buttonOne,buttonTwo,buttonThree,buttonFour,buttonFive,spaceOne,spaceTwo,spaceThree,spaceFour) ;
// Vertically align buttonOne to its superview however you like.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonOne]" options:0 metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths equal and >= 10. Make the buttons' widths equal.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[buttonOne][spaceOne(>=10)][buttonTwo(==buttonOne)][spaceTwo(==spaceOne)][buttonThree(==buttonOne)][spaceThree(==spaceOne)][buttonFour(==buttonOne)][spaceFour(==spaceOne)][buttonFive(==buttonOne)]|" options: NSLayoutFormatAlignAllCenterY metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths 30% of the NSButtons' widths.
[superView addConstraint: [NSLayoutConstraint constraintWithItem: spaceOne
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: buttonOne
attribute: NSLayoutAttributeWidth
multiplier: 0.3
constant: 0 ] ] ;