NSLayoutConstraint: Visual format syntax for NSLayoutAttributeCenterX?

I'd like to know if it is possible to express NSLayoutAttributeCenterX/NSLayoutAttributeCenterY in visual format syntax. Thanks.

No. When creating constraints from a VFL string using +[NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views:], you can specify options that align all of the views by their centers (e.g. NSLayoutFormatAlignAllCenterX), but that's not quite the same thing.

Sorry, I did not make my question clear.


My question is actually is: Is it possible to write visual format constraints that center a subview in its parent view?

No, it's not possible.

Accepted Answer

Like Ken said in his first reply, not really except as a side effect of adding constraints on the other axis. Suppose you had a view you wanted 20 points from the top and centered horizontally... you could do that with something like

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[subview]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];


but no, you can't add *just* the horizontal centering constraint by itself. You have to use constraintWithItem:...

NSLayoutConstraint: Visual format syntax for NSLayoutAttributeCenterX?
 
 
Q