---
title: "#118: Surviving Auto Layout Bugs 🐞📐"
url: https://littlebitesofcocoa.com/118-surviving-auto-layout-bugs
author: Jake Marsh
published: 2015-11-04
topics: ["Layout","Techniques","UIKit"]
description: "With additions like UIStackView, Auto Layout has matured into quite a powerful system for managing how our views are arranged on screen."
---

# #118: Surviving Auto Layout Bugs 🐞📐

By Jake Marsh, November 4, 2015. HTML: https://littlebitesofcocoa.com/118-surviving-auto-layout-bugs

With additions like **UIStackView**, _Auto Layout_ has matured into quite a powerful system for managing how our **views** are arranged on screen. When things go wrong though, it can sometimes be difficult to diagnose the specific cause of the issue. Today we'll look at a few techniques for making sense of the madness caused by... _Auto Layout Bugs!_ 🐞🔪🔪🔪

Most issues arise from <i>"Unsatisfiable Layouts"</i>. That's fancy-talk for <i>"two or more constraints you gave Auto Layout conflict with each other."</i> The solution will of course be different in every case, but here's some sensible things to ask ourselves when an error first occurs:

- Is `translatesAutoresizingMaskIntoConstraints` set to `false` on the **views** we're adding **constraints** to?

- Are the **priorities** of each of **constraint**, as well as **content hugging** and **compression resistance priorities** ([Bite #69](https://littlebitesofcocoa.com/69)) what we expect them to be?

- "Can this <i>required</i> **constraint** work at a `999` **priority**?" Remember, _Auto Layout_ will try to get as close to our desired result as possible, while still satisfying all other **constraints**.

## Identifiers

``` swift
constraint.identifier = "image-fixed-width"
```

Identifiers help us more easily spot the important bits in those giant log outputs _Auto Layout_ loves to show us. They can be added in code or in **Interface Builder**.

## Log Specific Constraints

``` swift
profileHeaderView.constraintsAffectingLayoutForAxis(.Vertical)
```

When debugging complex layouts, it can sometimes be helpful to look at <i>only</i> the **constraints** involving a specific problem **view** or area. We can use this **function** to grab an **array** of the **constraints** affecting a particular **axis**. Neat.

## Brain Surgery

When all else fails, don't be afraid to go in and start temporarily commenting-out or disabling **constraints** then observe the results. This can often lead to unexpected insights into how **constraints** are behaving.

## Practice!

A great way to fight _Auto Layout_ issues is to try to catch them before they happen. That means becoming more familiar with _Auto Layout_, which means practice. For example: When a question or issue comes up, create a new 'dummy' Xcode **project**. Throw some **views** and **constraints** in there and try it out. Tweak some **priorities**, observe their effects in the isolated environment. When in doubt, try it out!
