Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout and Device Orientation

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.

like image 687
rokridi Avatar asked Dec 03 '13 08:12

rokridi


People also ask

What is the use of Autolayout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

What are the two orientations that we can have for mobile devices?

The default orientation for most smartphones and for the iPad is portrait. However, landscape is the default for Android, Windows 8 tablets and BlackBerry's Playbook.

What is Autolayout in Swift?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.

What is iOS Autolayout?

Auto Layout is the preferred technology to define layouts for user interfaces on iOS and macOS. Its goal: To make it easy for you to create user interfaces that adapt automatically to different screen sizes and orientations.


1 Answers

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
}
like image 97
Fr4ncis Avatar answered Sep 20 '22 06:09

Fr4ncis