Autolayout and Device Orientation

旧街凉风 提交于 2019-11-30 20:09:07
Fr4ncis

I would use this UIViewController's method:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

that is called upon rotation, and call -setNeedsUpdateConstraints from there. If you need to do additional calculations or manage the contrainsts add

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    // do calculations if needed, like set constants
}

The best approach is to use neither. Instead, configure your constraints correctly so that no programmatic changes are required on rotation. The Auto Layout runtime will maintain the views in position as you already have specified.

Updating constraints other than changing the value of .constant is a real performance hit and should be avoided.

Using viewWillLayoutSubviews is not necessary. Auto Layout methods are updateViewConstraints (for the view controller), and updateConstraints (in the views).

I believe that the best approach is to update the constraints in -(void)updateViewConstraints by checking the device orientation. There is no need to call setNeedsUpdateConstraints in willAnimateRotationToInterfaceOrientation because it is automatically called by the iOs when the device orientation changes. Thank you all for the great effort.

for ios8+

From the documentation for:

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
          withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

A standard view controller might use this method to change the constraints on the views it manages.

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