Reputation: 7484
I need a view to be centered vertically when in portrait mode and moved to the right in landscape (say 150 pixels).
I've tried setting some constraints but can't seem to nail it down:
[myImage addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(577@700)-[contentView]-(257@800)-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(contentView)]];
NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:contentView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:myImage
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:-1];
myConstraint.priority = 700;
[myImage addConstraint:myConstraint];
This works in landscape mode, but not in portrait. In portrait, the left side constraint (570@700) fails, then the left side constraint can succeed (257@800) and the center X is never implemented. I need the first set (570@700 and 257@800) to both fail so it can resort to the center X constraint.
or should i just ditch the centerX constraint?
Upvotes: 2
Views: 803
Reputation: 4421
I see two options:
contentView
's CenterX = myImage
's CenterX, and save a reference to the Constraint. When the device rotates to landscape, set the Constraint's constant to 150; when rotated to portrait, set the constant to 0.contentView
's horizontal location & size Constraints, and add the appropriate Constraints for your device's orientation.The first option is simpler, but less future-proof if a new Apple device has different dimensions. If you go with the first option, you won't need the @"H:|-(577)-[contentView]-(257)-|"
Constraint, but you will need a Constraint to tell contentView
its horizontal size.
Let me know if you'd like me to elaborate on either option.
Upvotes: 2