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

- 5,821
- 2
- 26
- 40
-
6I was in the process of writing an answer, but yours is waay better. Nice work! – mattsven Apr 05 '16 at 19:34
-
To add, the UIStackView class https://developer.apple.com/reference/uikit/uistackview has entered the scene, @Andrew – finneycanhelp Oct 19 '16 at 15:10
-
1@Vinod The answer is excellent but maybe you could also mention `NSLayoutAnchor` as the new improvement that Apple recently added on top of `NSLayoutConstraint`. The `UIStackView` is also an excellent option for rapid prototyping of UI too, as @finneycanhelp said. – HuaTham Jan 29 '17 at 06:26
-
@Vinod I think that you should add priority property to your list of important properties of NSLayoutConstraint class. You can achieve a lot by combining constraints with different priority and also you can even animate views by changing its priority. ( e.g. change priority from high to low for constraint that defines height of your view allows you to appear/disappear your view on the screen ;-). And yes, it's animateable :-) ). BTW, very nice answer, thanks. – Vojta Jul 18 '17 at 09:51