If you want to center the red square horizontally, then you want to equate them to "CenterX", not CenterY. Thus, the restriction should look like this:
[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0.f]];
Then, with the first restrictions, the restrictions that you have there are ambiguous, since there is more than one way to satisfy the field >=0 on each side and <=400 in width. It would be best to say exactly what you said in your question, namely: you need a width that should be <= 400, and you would , like , field 0, if possible.
So something like this:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-( 0@900 )-[contentView(<=400)]-( 0@900 )-|" options:0 metrics:0 views:views]];
I believe you want what you want?
source share