---
title: "#9: Animating Constraints 👋"
url: https://littlebitesofcocoa.com/9-animating-constraints
author: Jake Marsh
published: 2015-05-28
topics: ["Animation","Layout","UIKit"]
description: "Animating between two sets of Auto Layout constraints is quite simple. All you have to do is update your installed/configured constraints and then call..."
---

# #9: Animating Constraints 👋

By Jake Marsh, May 28, 2015. HTML: https://littlebitesofcocoa.com/9-animating-constraints

Animating between two sets of **Auto Layout** constraints is quite simple.

All you have to do is update your installed/configured constraints and then call `layoutIfNeeded` inside of a `UIView.animateWith*` closure.

<center>
<img src="https://media.littlebitesofcocoa.com/bite-assets/009-animating-constraints-standalone.gif" style="max-width: 379px;" />
</center>

``` swift
class DoorsViewController: UIViewController {
    var open: Bool = false {
        didSet { transition() }
    }

    func transition() {
        self.view.layoutIfNeeded() // force layout before animating

        UIView.animateWithDuration(0.4) {
            // change constraints inside animation block
            self.updateConstraintsForDoors()

            // force layout inside animation block
            self.view.layoutIfNeeded()
        }
    }

    func updateConstraintsForDoors() {
        leftDoorHorizontalConstraint.constant = open ? -16 : -leftDoorView.bounds.size.width
        rightDoorHorizontalConstraint.constant = open ? -16 : -rightDoorView.bounds.size.width
    }
}
```

[Download sample project](http://j.mp/bite009)
