Angel F Syrus
Angel F Syrus

Reputation: 2042

Change UICollectionView cell X position - swift

How can I change the X position of every single cell programmatically? I can change the width and height of the cell as my wish but it is unable to complete the project because of XY position.

enter image description here

and my full view controller code is,

import UIKit

class ReviewVC: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{


    var cellWidthArr : [CGFloat] = [60.0 , 170.0 , 110.0 , 120.0]


    var titleArr = ["  Polite  " , "  Showed up on time  ", "  Quick responses  " , "  Fair prices  "]

    override func viewDidLoad() {
        super.viewDidLoad()


        self.navigationItem.title = "Review your experiance"

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView!.collectionViewLayout = layout

    }


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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReviewCollectionCell", for: indexPath)as? ReviewCollectionCell

        cell?.titleLabel.text = self.titleArr[indexPath.row]

        cell?.titleLabel.layer.cornerRadius = (cell?.titleLabel.bounds.size.height)!/2
        cell?.titleLabel.clipsToBounds = true

        cell?.titleLabel.layer.borderColor = (UIColor.init(red: 240/255, green: 36/255, blue: 70/255, alpha: 1.0)).cgColor
        cell?.titleLabel.layer.borderWidth = 1.0

        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: self.cellWidthArr[indexPath.row], height: 30)
    }
}

Anyone please help me to findout the solution.Thanks...

Upvotes: 3

Views: 1976

Answers (2)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4399

You can use the open source project TagCellLayout written by riteshhgupta in GitHub.

Sample Code to initialize the layout:

import TagCellLayout

let tagCellLayout = TagCellLayout(alignment: .left, delegate: self)
collectionView.collectionViewLayout = tagCellLayout

enter image description here

I know you might write the lots of code for this, but it would be helpful greatly and even you can customize to different layout alignments.

Upvotes: 2

Ruban4Axis
Ruban4Axis

Reputation: 841

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return (ArrayName[indexPath.row] as NSString).size(withAttributes: nil)
}

Rather than giving Cell Fixed Size in an Array there is a way to find label size in sizeForItemAtIndexPath return the size of the text So it will find the size and work

Upvotes: 1

Related Questions