Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to programmatically update constraints when the device orientation changes?

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

like image 493
AppsDev Avatar asked Nov 08 '15 17:11

AppsDev


People also ask

How to update view constraints?

To schedule a change, call setNeedsUpdateConstraints() on the view. The system then calls your implementation of updateConstraints() before the layout occurs. This lets you verify that all necessary constraints for your content are in place at a time when your custom view's properties are not changing.

When should I call updateConstraints?

Sounds simple enough: whenever you need to change your layout, invalidate it. Then wait for the system to call updateConstraints() in the next layout pass, which is your chance to make the necessary changes to your layout constraints.


1 Answers

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
    }];
}
like image 61
themobileapper Avatar answered Nov 15 '22 04:11

themobileapper