UICollectionView Supplementary Views & Highlighting Behaviour

Not as simple as you’d expect

Greg Gunner
Apr 8, 2014 · 2 min read

Maybe we’re spoilt by UITableView or maybe we’re graced with it, either way UICollectionViews are not as well rounded.

I needed to make the supplementary views in my UICollectionView behave like UITableViewCells do when you touch down on them, i.e. support a background colour and a method call when it happens.

Like this

UICollectionViews don’t really support this behaviour at all, I had previously added a single UIGestureRecognizer to each supplementary view so I knew when they were tapped but it didn’t allow me to do the correct highlighting behaviour.

Here’s what I did:

1. Subclass UICollectionView

Medium’s code block isn’t friendly with this volume of text

https://gist.github.com/GregularExpressions/10114517


2. Create a setHighlighted method in your supplementary view

Changing the background colour and setting subviews to 50% opacity

https://gist.github.com/GregularExpressions/10115164


3. Fix any existing CollectionView wide gestures

longPressGesture.delaysTouchesBegan = NO;

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store