Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In iOS, what's the difference between autoresizing, AutoLayout, and constraints?

Tags:

ios

In reading through the Apple documentation, I find references to autoresizing, AutoLayout, and constraints. What's the difference between using all of these in code? What is the correct way to apply these techniques programmatically in iOS 9?

like image 400
Andrew Avatar asked Apr 05 '16 19:04

Andrew


People also ask

What is IOS AutoLayout?

Auto Layout is a constraint-based layout system. It allows developers to create an adaptive interface that responds appropriately to changes in screen size and device orientation.

What is Autoresizing in IOS?

Autoresizing masks is a layout method using a bit mask, or an encoded set of flags, that defines how a view should respond to changes in the superview's bounds. The properties available in a autoresizing mask on a view are: Flexible Top Margin, meaning resizing can change the view's top margin.

What is AutoLayout in IOS 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 AutoLayout?

Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.


1 Answers

There are really just two things here:

  • Autoresizing
  • AutoLayout

Autoresizing is basically a collective term for the old way Apple introduced in order to enable developers to build dynamic layouts. The number one usecase to address here was screen rotation. Since when a screen would be rotated (or otherwise resized), the subviews in the screen would most likely hold an incorrect frame (position and size) in the newly sized superview. To address this, Apple introduced a series of enumerable properties (called Autoresizing Masks), that tell the superview to treat a subview in a particular way. Among these are:

  • Flexible Width/Height, which causes a subview to expand to the fullest width/height available

  • Flexible Leading/Trailing/Top/Bottom space, which allows a specific edge to be variable, and so forth.

A view could contain any combination of these enum properties.

This was inadequate because, among other shortcomings, it lays down no rules regarding how a view should be layouted (if that's a verb) with respect to its other sibling views. It also required a lot of extra coding to manually resize views on orientation changes.

Here's where AutoLayout entered the picture. Apple built a framework which worked on the basis of constraints - rules that could be applied on views and between views, that would determine how a view would be sized in variable screen sizes. These constraints are structured in a class called NSLayoutConstraint, and each instance (constraint) of this class has the following important properties:

  • The item (view) on which the constraint is applied
  • The property of the view (height, width, leading edge, trailing edge, and so on) that the constraint is applicable to
  • The second item (a sibling or a child or a parent view) to which the constraint is related
  • The second item's attribute
  • The multiplier on the constraint: useful in order to specify ratio based constraints
  • A value (or constant) of the constraint: interestingly, the only property of a constraint that can be changed after instantiation.

A simple example of an NSLayoutConstraint, stated prosaically is: a view's width will be half the the width of its superview multiplied by 60%.

Your AutoLayout based UI would consist of many such constraints, which will all work together to express an unambiguous and non-conflicting UI Layout.

Now the AutoLayout engine, which makes it all work, interacts with views on the screen, calling AutoLayout messages such as layoutSubviews whenever needed in order automatically resize (layout) views whenever changes occur on screen, such as orientation change, a superview getting resized etc.

Constraints are most commonly added by InterfaceBuilder (.xib and .storyboard files), but adding them by code entails the same principle: create an instance of NSLayoutConstraint and add it to the highest view applicable (for eg., if there's a constraint between a child and a parent view, the constraint should be added to the parent view. If there's a constraint between two subviews, again, add it to the parent.)

Apple's AutoLayout guides, API documentation and introductory WWDC videos on AutoLayout are excellent, and those would be the best places to learn more.

like image 159
Vinod Vishwanath Avatar answered Sep 28 '22 05:09

Vinod Vishwanath