Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : How to set Custom UICollectionViewCell as circle?

I've custom UICollectionViewCell that contains UILabel and UIImage. I want to set the cell in circular format.

Note:- sizeForItemAtIndexPath cannot be changed due to AutoLayout.

Below is code that I've used :

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath ) -> CGSize {

          return CGSize(width: (self.objKeypadCollectionView.frame.size.width/3)-10, height: (self.objKeypadCollectionView.frame.size.height/4)-10)
    }

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let objKeypadCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("idKeypadCollectionViewCell", forIndexPath: indexPath) as! KeypadCollectionViewCell

        if (self.numberArray[indexPath.item])=="asda" {
            objKeypadCollectionViewCell.lblNumber.text = ""
            objKeypadCollectionViewCell.imgPrint.hidden = false
        }
        else if (self.numberArray[indexPath.item])=="Derterel" {
            objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
            objKeypadCollectionViewCell.imgPrint.hidden = true
        }
        else {
            objKeypadCollectionViewCell.imgPrint.hidden = true
            objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
            objKeypadCollectionViewCell.layer.borderColor = UIColor.lightGrayColor().CGColor
            objKeypadCollectionViewCell.layer.borderWidth = 1
            objKeypadCollectionViewCell.layer.cornerRadius = objKeypadCollectionViewCell.frame.size.width/2
        }
        return objKeypadCollectionViewCell
    }

Screenshot 1

Design screen

like image 314
Jayprakash Dubey Avatar asked Sep 01 '16 13:09

Jayprakash Dubey


4 Answers

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.

like image 106
onCompletion Avatar answered Nov 15 '22 21:11

onCompletion


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.

like image 44
Sanoj Kashyap Avatar answered Nov 15 '22 21:11

Sanoj Kashyap


The problem is cell's width and height is changed at run time according to your screen width, So to solve this issue add one square UIView inside your cell and use that UIView inside cellForItemAtIndexPath method to make it circle view like this.

objKeypadCollectionViewCell.circleView.layer.borderColor = UIColor.lightGrayColor().CGColor
objKeypadCollectionViewCell.circleView.layer.borderWidth = 1
objKeypadCollectionViewCell.circleView.layer.cornerRadius = objKeypadCollectionViewCell.frame.size.width/2
objKeypadCollectionViewCell.circleView.layer.masksToBounds = true
like image 28
Nirav D Avatar answered Nov 15 '22 21:11

Nirav D


By giving this line for calculating the height of your cell

return CGSize(width: (self.objKeypadCollectionView.frame.size.width/3)-10, height: (self.objKeypadCollectionView.frame.size.height/4)-10)

You cant achieve what you want. Use Aspect Ratio technique. For Height and width of cell use your screen width and based on that aspect ratio update your cell height and width then and only then this line works....

If you need clarification skype me iammubin38

like image 41
Mubin Mall Avatar answered Nov 15 '22 21:11

Mubin Mall