Swift : How to set Custom UICollectionViewCell as circle?

This is my UICollectionView Example which you can check for your solution. I am using storyboard to setup the primary collectionView with AutoLayout. Also, attaching some screenshots to clarify the result.

There is a method called drawRect(). You can use that inside your collectionView Cell class for doing the UI stuffs.

Now here is my code.

1. ViewController.swift //Just the normal UIViewController like yours, nothing else... :)

//
//  ViewController.swift
//  CollectionCheckCircle
//
//  Created by Tuhin Samui on 02/09/16.
//  Copyright © 2016 Tuhin Samui. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 200
    }


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cellOfCollection = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollectionView", forIndexPath: indexPath) as! CollectionViewCell

        return cellOfCollection
    }

}

2. CollectionViewCell.swift //Just a UICollectionViewCell class for dequeuing the cell.

//
//  CollectionViewCell.swift
//  CollectionCheckCircle
//
//  Created by Tuhin Samui on 02/09/16.
//  Copyright © 2016 Tuhin Samui. All rights reserved.
//

import UIKit

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!

    override func drawRect(rect: CGRect) { //Your code should go here.
        super.drawRect(rect)
        self.layer.cornerRadius = self.frame.size.width / 2
    }

}

Note: I am not using either any flowlayout of UICollectionViewFlowLayout or cell size inside sizeForItemAtIndexPath for this collectionView. You can add those things too. Please do some experiments as per your needs. BTW I am using Xcode 7.3.1 and Swift 2.2.

Here is the storyboard screenshot for collectionView setup and result on the simulator. First Storyboard screenshot with simulator result

Second Storyboard screenshot with simulator result

Hope this helped. Sorry for any mistake.


Just try to give corner radius as height/2, make sure cell should have same height and width.

cell.layer.cornerRadius = min(cell.frame.size.height, cell.frame.size.width) / 2.0

cell.layer.masksToBounds = true

Please, let me know if this works for you.