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

我怕爱的太早我们不能终老 提交于 2020-01-13 09:01:26

问题


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


回答1:


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
    }];
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/33597133/correct-way-to-programmatically-update-constraints-when-the-device-orientation-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!