Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on
constraints placed on those views. For example, you can constrain a button so that it is horizontally centered
with an Image view and so that the buttons top edge always remains 8 points below the images bottom. If the
image views size or position changes, the buttons position automatically adjusts to match.
This constraint-based approach to design allows you to build user interfaces that dynamically respond to both
internal and external changes.
External Changes
External changes occur when the size or shape of your superview changes. With each change, you must
update the layout of your view hierarchy to best use the available space. Here are some common sources of
external change:
Internal Changes
On This Page
Internal changes occur when the size of the views or controls in your user interface change.
Here are some common sources of internal change:
change both the height and the width of any textual elements in your user interface. If the user changes the
font size while your app is running, both the fonts and the layout must adapt.
To lay out your user interface, you had to calculate the size and position for every view in your view hierarchy.
Then, if a change occurred, you had to recalculate the frame for all the effected views.
In many ways, programmatically dening a views frame provides the most exibility and power. When a
change occurs, you can literally make any change you want. Yet because you must also manage all the
changes yourself, laying out a simple user interface requires a considerable amount of effort to design, debug,
and maintain. Creating a truly adaptive user interface increases the difculty by an order of magnitude.
You can use autoresizing masks to help alleviate some of this effort. An autoresizing mask denes how a
views frame changes when its superviews frame changes. This simplies the creation of layouts that adapt to
external changes.
However, autoresizing masks support a relatively small subset of possible layouts. For complex user
interfaces, you typically need to augment the autoresizing masks with your own programmatic changes.
Additionally, autoresizing masks adapt only to external changes. They do not support internal changes.
Although autoresizing masks are just an iterative improvement on programmatic layouts, Auto Layout
represents an entirely new paradigm. Instead of thinking about a views frame, you think about its
relationships.
Auto Layout denes 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.
On This Page
The logic used to design a set of constraints to create specic behaviors is very different from the logic used to
write procedural or object-oriented code. Fortunately, mastering Auto Layout is no different from mastering any
other programming task. There are two basic steps: First you need to understand the logic behind constraintbased layouts, and then you need to learn the API. Youve successfully performed these steps when learning
other programming tasks. Auto Layout is no exception.
The rest of this guide is designed to help ease your transition to Auto Layout. The Auto Layout Without
Constraints chapter describes a high-level abstraction that simplies the creation of Auto Layout backed user
interfaces. The Anatomy of a Constraint chapter provides the background theory you need to understand to
successfully interact with Auto Layout on your own. Working with Constraints in Interface Builder describes the
tools for designing Auto Layout, and the Programmatically Creating Constraints and Auto Layout Cookbook
chapters describe the API in detail. Finally, the Auto Layout Cookbook presents a wide range of sample
layouts of varying levels of complexity, you can study and use in your own projects, and Debugging Auto
Layout offers advice and tools for xing things if anything goes wrong.
Copyright 2016 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-03-21
On This Page