Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select and highlight a UICollectionView like a UITableViewCell?

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.

like image 710
Minebomber Avatar asked Oct 20 '15 01:10

Minebomber


People also ask

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.


1 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.

like image 105
Nicolas Miari Avatar answered Sep 28 '22 22:09

Nicolas Miari