为什么NSLayoutAttributeCenterX与NSLayoutAttributeWidth的"无效配对"

Mob*_*Vet 9 autolayout ios6 nslayoutconstraint

我试图让自动布局管理器根据超级视图的宽度调整视图的中心点.我不明白为什么这是属性的"无效配对"(由崩溃和NSInvalidArgumentException告知)

UIView *ac;
NSLayoutConstraint *cXloc = [NSLayoutConstraint constraintWithItem:ac 
                                                         attribute:NSLayoutAttributeCenterX 
                                                         relatedBy:NSLayoutRelationEqual 
                                                            toItem:ac.superview 
                                                         attribute:NSLayoutAttributeWidth 
                                                        multiplier:.1 
                                                          constant:x*ac.superview.frame.size.width*.2];
[ac.superview addConstraint:cXloc];
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么这是一个'无效的削减'以及我应该如何处理这个问题?谢谢

Joh*_*uer 3

如果将 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 ] ] ;
Run Code Online (Sandbox Code Playgroud)