I am trying to have the auto layout manager adjust the center point of a view, based on the width of the super view. I don’t understand why that is an ‘Invalid Pairing’ of the attributes (as told by the crash and 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];
Could someone explain why this is an ‘Invalid paring’ and how I should approach this?
Thanks
If you relate ac’s AttributeCenterX to its superview’s AttributeCenterX, AttributeLeading, or AttributeTrailing, you should be able to express your desired constraint using the multiplier and constraint. Keep in mind that the constant is evaluated only when the constraint is created, and your example’s constant wouldn’t update as ac.superview’s width changes.
If you can express in words how you’d like ac positioned relative to its superview, we can suggest a constraint.
Edit
Here’s an example with 5 NSButtons. They themselves and the space between them expand so that the spaces are 30% as wide as the buttons, all the buttons have the same width, and all the spaces have the same width. Creating 4 invisible NSViews just for spacing is pretty cumbersome, especially considering you’ve got it working outside of autolayout. But in case you’re curious: