kodeco.com Forums

Video Tutorial: Custom Collection View Layouts Part 1: Pinterest – Basic Layout

Learn how to implement the basic Pinterest layout.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3987-custom-collection-view-layout/lessons/2

Thanks for the great video and your detailed explanation.

I have slighlty improved the given code for myself:

  • using guard statements to improve readability and prevent the “pyramid of doom”
  • I’ve extracted the random color generation from the controller to the data model (its now a lazy initialized property).


I’m trying to use collection view not with a UICollectionViewController, but just with a UIViewController conforming to UICollectionViewDataSource and UICollectionViewDelegate. I’ve setup everything manually (setting the data source and delegate of the collection view in the storyboard).

When I got to the final step of this tutorial and wanted to initialize PinterestLayout in viewDidLoad, I get an error Use of unresolved identifier ‘collectionViewLayout’. Can you help me figure out what I’m doing wrong? Maybe I need to conform to another class?

If this is not a best practice to not use UICollectionViewController also please let me know. :smile: I learned this approach from table views.

Update: After the initial frustration of not knowing how to proceed. Today I tried again and it now works! I discovered I need to setup an IBOutlet to the collection view, so I can do:
let layout = myCollectionView.collectionViewLayout as! GridLayout
I’m really glad I got it to work, now I can continue experimenting with custom layouts. Thanks for the great videos so far!

Hi, I used the concepts in this tutorial in my app. The app would occasionally crash complaining that there weren’t enough attributes. According to the definition of layoutAttributesForElementsInRect() and the tutorial you’re suppose to return the attributes that are within the rect and that’s what I was doing.

I did some research and found an Apple example Collection View Transitions where it returns the entire array of cached attributes. This collection view has items beyond the size of the device so I would have expected that it return only a subset of the attributes.

I changed my app to return all cached attributes (ignoring the rect) and now everything works fine.

Any idea why, apparently sometimes, all cached attributes are needed?

I guess I found another twist to all this … The Apple doc states that you must implement layoutAttributesForItemAtIndexPath() as in:

Every custom layout object is expected to implement the layoutAttributesForItemAtIndexPath: method.

As in:
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { return cache[indexPath.item]; }
This was not implemented in the tutorial. Adding the implementation of this also eliminates the crash.


will this video series will be converted to swift 2.2 or later, I would like to give it a try

after converting it to swift 2.2 I got compile error and I am not able to execute the project


Hi, can you provide updated Swift files based on Swift 3? I download the projects but failed to compile due to errors. Thank you.

you can run the code in “legacy mode”. Just go to “Build Settings → Use Legacy Swift Language Version” You have to fix a couple of compile errors though

Can we have an updated version of the Custom CollectionView Layout series with the new overridable methods. The overridable methods mentioned in this video are not available in the latest version of swift.

3 items in the row does not contain equal leading and trailing space. Why does the section inset add unequal leading and trailing spacing for the first item? Explanation is not clear.

@skyrocketsw Can you please help with this when you get a chance? Thank you - much appreciated! :]