Mike Sabatini's answer works fine if you set cell properties directly in collectionView cellForItemAt, but if you try to set them in awakeFromNib () of a custom subclass of UICollectionViewCell, you will have the wrong bezierPath installed on devices that don't work. does not match the width and height previously set in the storyboard (IB).
The solution for me was to create a function inside a subclass of UICollectionViewCell and call it from cellForItemAt as follows:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as? CustomCollectionViewCell{ cell.configure()) return cell } else { return UICollectionViewCell() } }
And on CustomCollectionViewCell.swift:
class CustomCollectionViewCell: UICollectionViewCell{ func configure() { contentView.layer.cornerRadius = 20 contentView.layer.borderWidth = 1.0 contentView.layer.borderColor = UIColor.clear.cgColor contentView.layer.masksToBounds = true layer.shadowColor = UIColor.black.cgColor layer.shadowOffset = CGSize(width: 0, height: 2.0) layer.shadowRadius = 2.0 layer.shadowOpacity = 0.5 layer.masksToBounds = false layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath} }
frantenerelli Oct 26 '18 at 4:29 2018-10-26 04:29
source share