Search code examples
swiftuicollectionviewradius

How to make an image radius in a collection view?


I would like to make a radius for my collection view to make the pictures a little more round. I have tried adding in the viewdidload() function the following:

mine.photo.layer.cornerRadius = 10

mine is another class that I have with the IBOutlet photo: UIImageView!.

What I have to make my collection view neat are the following functions:

extension DiscoverViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //dude.photo.layer.cornerRadius = 10
        return CGSize(width: collectionView.frame.size.width / 2 - 2.5, height: collectionView.frame.size.width / 1.5 - 1 )

    }
}

Any ideas?


Solution

  • The round you need should be inside the cell class

    class CellColl:UICollectionViewCell {
     @IBOutlet weak var photo: UIImageView!
       override func awakeFromNib() {
          super.awakeFromNib()
          self.photo.layer.cornerRadius = 10
          self.photo.clipsToBounds = true
       }
    }