Here i am trying highlight the UICollectionViewCell on selection as shown in the image.I tried adding the border to the selected cell and the border comes inside the cell content view. Here is my try:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
let borderWidth: CGFloat = 6
cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))!
cell?.contentView.layer.borderColor = cell?.backgroundColor?.cgColor
cell?.contentView.layer.borderWidth = borderWidth
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
let borderWidth: CGFloat = 0
cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))!
cell?.contentView.layer.borderColor = UIColor.clear.cgColor
cell?.contentView.layer.borderWidth = borderWidth
}
How to do this?