---
title: "#307: Animating Collection View Layout Changes 📱🚶🏼🏃🏼"
url: https://littlebitesofcocoa.com/307-animating-collection-view-layout-changes
author: Jake Marsh
date: 2017-04-05
published: 2017-04-05
topics: ["Animation","Collection Views"]
description: "Today we'll continue our look at UICollectionView by diving into animation. Let's begin. We'll start with the same simple baseline collection view..."
---

# #307: Animating Collection View Layout Changes 📱🚶🏼🏃🏼

By Jake Marsh, April 5, 2017. HTML: https://littlebitesofcocoa.com/307-animating-collection-view-layout-changes

Today we'll continue our look at _`UICollectionView`_ by diving into **animation**. Let's begin.

We'll start with the same simple baseline **collection view controller** we made at the beginning of [Bite #306](https://littlebitesofcocoa.com/306-customizing-collection-view-cell-insertion-animations). 

Now, we'll add two new _collection view layouts_ to our **view controller**:

``` swift
var small: UICollectionViewFlowLayout = {
  let layout = UICollectionViewFlowLayout()
  
  layout.itemSize = CGSize(width: 75, height: 75)

  return layout
}()

var big: UICollectionViewFlowLayout = {
  let layout = UICollectionViewFlowLayout()
  
  layout.itemSize = CGSize(width: 150, height: 150)

  return layout
}()
```

Then, we'll override `init` on our **view controller**, and hand it our <i>"small"</i> layout:

``` swift
init() {
  super.init(collectionViewLayout: small)
}
```

Nice. At this point we have a basic _collection view controller_, scrolling our small **cells**:

<img src="https://media.littlebitesofcocoa.com/307-1.gif" style="max-width: 375px; border-radius: 4px;" />

Now, we'll add some code to toggle between our two **layouts** when the user taps any cell:

``` swift
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  let newLayout = (collectionView.collectionViewLayout == small ? big : small)

  collectionView.setCollectionViewLayout(newLayout, animated: true)
}
```

Now we can toggle back and forth between our two layouts with a nice looking animation. Very cool.

<img src="https://media.littlebitesofcocoa.com/307-2.gif" style="max-width: 375px; border-radius: 4px;" />

It's super handy how the `setCollectionViewLayout` **function** is smart enough to alter the animtion so the **cell** we selected is still in view once the animation completes.

We can see this effect exaggerated when we bump up the `itemSize` of our <i>"big"</i> **layout**:

``` swift
var big: UICollectionViewFlowLayout = {
  let layout = UICollectionViewFlowLayout()
  
  layout.itemSize = CGSize(width: 300, height: 300)

  return layout
}()
```

<img src="https://media.littlebitesofcocoa.com/307-3.gif" style="max-width: 375px; border-radius: 4px;" />

Nice!

[Download the Xcode project we built in this Bite right here](https://media.littlebitesofcocoa.com/307-project.zip).

One final note here: This just one approach. There are <i>tons</i> of different ways to achieve this (and similar) behavior. We'll take a look at more as we dive deeper into _collection views_ in upcoming _Bites_.

Have a specific _`UICollectionView`_ question you'd like answered? [Send it along!](mailto:hello@littlebitesofcocoa.com).

<p></p>
