5

I am using autolayout with a constraint. The top space to superview is 100

I now want to change this so that when the user rotates the device to landscape The top space to superview is 50

Can this be done at design time?

How can this be done at runtime?

1 Answer 1

19

Well, if your 50 px for landscape and 100 for portrait can somehow be derived by relations to sibling subviews and fixed offsets to the bounds of superview, then yes, this can be done "at design time", and that's actually what auto layout is meant to be for.

Otherwise you would need to have a reference to the constraint, then in willRotateToInterfaceOrientation:duration: method you need to change the value of the constant of this constraint and call layoutIfNeeded for the superview.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    self.myConstraint.constant = (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) ? 50. : 100.;
    [self.someView layoutIfNeeded];
}

(written in the browser, might have mistakes)

Sign up to request clarification or add additional context in comments.

1 Comment

i just need to know how attribute value to a NSLayoutConstraint. Thanks for your help NSLayoutConstraint.constant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.