Autolayout and Device Orientation
Asked Answered
W

4

7

I am developing an application which supports portrait and landscape modes. I am using auto layout to arrange my views. As i have been reading many posts and i have realized that developers commonly use one among the following approaches.

1. First approach:

Implement the UIViewController:updateConstraints method and update constraints according to device orientation.

2. Second approach:

Implement the UIViewController:viewWillLayoutSubviews method and update constraints according to device orientation.

Could anyone please tell me what is the best approach to use ? I have been searching for a best practice to combine autorotation and auto layout and nothing yet. Thanks.

Willdon answered 3/12, 2013 at 8:52 Comment(0)
P
6

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
}
Princessprinceton answered 3/12, 2013 at 9:39 Comment(4)
I believe there is no need to calculate constraints in willAnimateRotationToInterfaceOrientation method because this can be done in the updateViewConstraints by checking the current device orientation.Willdon
maybe my answer is not clear, I meant that the calculations are to be done in updateViewContraints method.Princessprinceton
I confirm that the calculations are to be done in updateViewContraints method. But when i call setNeedsUpdateConstraints method in willAnimateRotationToInterfaceOrientation method i realize that updateViewConstraints is called more than once.Willdon
willAnimateRotationToInterfaceOrientation is depreciated. Use "viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)" instead. But consider that updateViewConstraints will be called before the view changes.Elnora
F
5

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).

Fluting answered 3/12, 2013 at 9:47 Comment(4)
But sometimes configuring the constraints is not enough because the layout aspect can totally change when switching from landscape to portrait and vice versa.Willdon
in that case, you will need one of the update constraints methods as described in the answer above by @PrincessprincetonFluting
just saw this come up. It might be useful for you: #17773422Fluting
Yes i have just seen it and i think it is similar to @Princessprinceton solution.Willdon
W
1

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.

Willdon answered 3/12, 2013 at 10:40 Comment(0)
L
0

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.

Longtin answered 23/10, 2015 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.