Where to highlight UICollectionViewCell: delegate or cell?

后端 未结 7 2525
渐次进展
渐次进展 2020-12-13 09:43

According to the Collection View Programming Guide one should handle the visual state of the cell highlights in the UICollectionViewDelegate. Like this:

7条回答
  •  -上瘾入骨i
    2020-12-13 10:02

    Notice that UICollectionViewCell has a selectedBackgroundView property. By default, it's nil. Just create a view for this property, and it will appear when the user touches the cell.

    override func awakeFromNib() {
        super.awakeFromNib()
    
        let view = UIView(frame: contentView.bounds)
        view.isUserInteractionEnabled = false
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.backgroundColor = UIColor(white: 0.94, alpha: 1.0)
        selectedBackgroundView = view
    }
    

提交回复
热议问题