UICollectionView Supplementary Views & Highlighting Behaviour

Not as simple as you’d expect


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

Given that there’s no more convenient way to do it we’ll need to override UIResponder’s touchesBegan, touchesEnded and touchesCancelled within our UICollectionView subclass:

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

This is what the touches methods are calling to notify your supplementary view it’s being touched.

Changing the background colour and setting subviews to 50% opacity

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


3. Fix any existing CollectionView wide gestures

I had a long press gesture on the whole UICollectionView that I was using for some cell behaviour, I needed to set;

longPressGesture.delaysTouchesBegan = NO;
Show your support

Clapping shows how much you appreciated Greg Gunner’s story.