Adding a Decoration View to a UICollectionView in Interface Builder

tl;dr: You Can’t. But…

My current project employs multiple UICollectionViews with custom layouts. Those layouts have a nice decoration view that I borrowed from Joe Keely and Kyle Richter (repo here), which puts a sleek, modern IKEA style ‘shelf’ behind the collectionView’s

Saweeet UIDecorationView
Saweeet UIDecorationView

cells (i.e., the ‘books’).

However, I kept getting an error, which the debugger translated as “You need to register your decoration view as a class or a nib, or prototype it in storyboard.” This, I was happy to do, except that storyboard does not provide decorationView protoyping, just header/footer views, and of course, the collectionViewCells themselves.

…you can register them in initWithCoder.

Imagine my surprise when despite all my efforts to register the durn view, Xcode was not in the least impressed. To make a long story short, I remembered that when initializing an object in Storyboard, initWithCoder is called (rather than init). I do not recall exactly which Apple docs gave me this nugget (please let me know in the comments), but I believe it was some chain of hyperlinks bouncing off NSCopying and NSCoder protocols.

In hindsight, there it is, clear as day, on SO. But duplication of answers to debug questions is (often) Good for the Internet, providing a larger bullseye for a questioner to hit. Happy Bug Hunting y’all!

Leave a Reply

Your email address will not be published. Required fields are marked *