9

I'm using storyboard and autolayout, and setting the constraints in IB as IBOutlet in the corresponding view controller. I'm reading several posts regarding how to update the constraints to be different in portrait and in landscape but I'm still not sure of how should I do this:

  1. Should I set the new constraints in -viewWillTransitionToSize:withTransitionCoordinator: method, or in updateViewConstraints method?
  2. When the new constraints are set, should I call [self.view setNeedsUpdateConstraints];, or layoutIfNeeded, or setNeedsLayout?
  3. How should I update, for example, the constant of a certain constraint:

    self.myConstraint.constant = 30.0

or doing:

[self.view removeConstraint:self.passwordViewHeight];

self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute
                                                     multiplier:1.0
                                                       constant:34.0];

[self.view addConstraint:self.passwordViewHeight];

Thanks in advance

3 Answers 3

2

Orientation change be detected using the method viewWillTransitionToSize. This method will be called when the device orientation is about to change.

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator (id<UIViewControllerTransitionCoordinator>)coordinator{
      //change constraints
}

Alternatively, if you want to change the constraints after the orientation changes, use the coordinator object's animateAlongsideTransition method in the viewWillTransitionToSize method.

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        //change constraints
    }];
}
Sign up to request clarification or add additional context in comments.

Comments

1

The best way would be to change the constant instead of removing the constraint and recreating it again. Just adding or subtracting from the constant is much faster in the long run as well. What you would want to do is something like:

[self.view layoutIfNeeded];
self.myConstraint.constant = 30.0;
[self.view layoutIfNeeded];

It is recommended that you call layoutIfNeeded before and after you change the constant as well. This is because you may have some constraint that has not yet been done and will do so then, before you change more constraints.

4 Comments

Thank you. From which method should I call that?
You can make your own method to call that from, if you have set up a notification for the device orientation change call it from that method. If you haven't set that up, you will definitely want to do so with the following code: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethodHere) name:UIDeviceOrientationDidChangeNotification object:nil];
Would be viewWillTransitionToSize method also a good place?
That might work depending on how you are using it, but using that notification will guarantee that your method is called every time your device changes orientation
-1

The best idea will be use Size Classes in Interface Builder to achieve autorotation change.

https://www.codefellows.org/blog/size-classes-with-xcode-6-one-storyboard-for-all-sizes

1 Comment

Thanks. I need to support iOS 7 and above for iPhone devices and I already have a storyboard with only autolayout enabled. I have around 32 scenes in this storyboard... Is it worth to move to size classes at this point, and taking into account that I have to support iOS 7?

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.