I have a simple dilemma. I have a UICollectionViewController in my app that displays saved quizzes. I have the loading right, but I want it to move detect a tap on one of them and then do logic to load it into the main VC. I would like the selection to be similar to a UITableViewCell where it highlights it for a second and then unhighlights it. I am using a subclassed UICollectionViewCell if that helps.
3 Answers
In order for the highlighting to show, you can subclass your cells like this:
import UIKit
class CustomCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let backgroundView = UIView(frame: CGRectZero)
backgroundView.backgroundColor = UIColor.blueColor()
self.selectedBackgroundView = backgroundView
}
}
From the docs for UICollectionViewCell
:
You can use this view to give the cell a custom appearance when it is selected. When the cell is selected, this view is layered above the backgroundView and behind the contentView.

- 16,006
- 8
- 81
- 189
-
Thanks! I was wondering what those outlets were. And thanks for a code example. – Minebomber Oct 20 '15 at 13:08
-
There was a mistake in my original answer (assigning a `UIColor` value to a `UIView` property). Somebody suggested a correct fix but seems to have been rejected. I have edited the answer to fix it. – Nicolas Miari Feb 23 '17 at 01:36
-
This answer does not seem to work any more in Xcode 12.2 on Big Sur 11.1 targeting iOS 14.0. I get "Instance member 'selectedBackgroundView' cannot be used on type 'My_CollectionViewCell'". – Bob Peterson Dec 27 '20 at 18:29
UICollectionViewCell has a property
@property(nonatomic, getter=isHighlighted) BOOL highlighted;
You can use this property for selecting the UICollectionViewCell in cellForItemAtIndexPath:

- 1,444
- 15
- 24
-
How would I go about doing that? I'm new to UICollectionViews and had 10 trys just to get the cells working – Minebomber Oct 20 '15 at 01:46
-
Can you let me know whats the criteria for you to highlight the cell (User selected item ?). So i can post a answer in more detail way. – Sneha Oct 20 '15 at 03:14
-
Just like selecting a (default) uitableviewcell. It turns blue for a moment and becomes normal. Also selecting it similar to a button would be cool too. (You know, with the shadow color and stuff) – Minebomber Oct 20 '15 at 03:41
Unlike UITableView, in UICollectionView there is no specific method defined to do so. U need to implement it programmatically by changing its background colour or background image of the specific cell for instant. U can refer Apple Documentation.https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/ Hope it helps.
*****EDITED***** You can refer this ans too.. Why UICollectionView's UICollectionViewCell is not highlighting on user touch? Happy Coding.. :)

- 1
- 1

- 2,731
- 1
- 12
- 19