KevinB
KevinB

Reputation: 2484

Collection View with corner radius in Swift

I have a CollectionViewController and I would like to set corner radius to the CollectionView (the part below the header view).

enter image description here

This is what I would like (the corner radius in yellow). Is it possible? Do you have any idea about how I could do that?

Upvotes: 0

Views: 4377

Answers (3)

Ednei F Neto
Ednei F Neto

Reputation: 1

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
    
    cell.viewCell.layer.cornerRadius = 25.0
    cell.viewCell.clipsToBounds = true
    
    return cell
}

Upvotes: 0

KevinB
KevinB

Reputation: 2484

The solution was quite simple. I have just removed the inset on the top of the collectionViewLayout and added a new view to the HeaderView. After that, I applied the corner radius to the view added and it worked :)

Upvotes: 1

Sabarinathan Jayakodi
Sabarinathan Jayakodi

Reputation: 342

        extension UICollectionView {
    func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }}

   collectioniew.roundCorners([.topLeft,.topRight], radius: 5)

Upvotes: 2

Related Questions