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

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


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.