Hamza
Hamza

Reputation: 151

Issue while collection view cell scroll to center in swift

I have a collection view in my VC, I scroll collection view horizontally, I want to center each cell on scroll horizontally, I have tried a code but when it scrolls it shows next cell more left then the second one .This is my code,

fileprivate let sectionInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)

gridView.delegate = self
    gridView.dataSource = self
    gridView.register(ItemCollectionViewCell.self, forCellWithReuseIdentifier: "ItemCollectionViewCell")
    gridView.register(UINib(nibName: "ItemCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ItemCollectionViewCell")
    gridView.showsHorizontalScrollIndicator = true
    gridView.bounces = true
    gridView.showsHorizontalScrollIndicator = false
    gridView.showsVerticalScrollIndicator = false

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return auctions?.properties?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell:ItemCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! ItemCollectionViewCell;
    cell.propertyData = (auctions?.properties![indexPath.row])!
    if auctions?.status == AuctionStatus.Live.rawValue {
        cell.noOfBidsPlaced.isHidden = false
    }
    else {
        cell.noOfBidsPlaced.isHidden = true
    }
    cell.populateCell()

    return cell
}


func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let size = CGSize(width: 335 , height: 337)
    return size
}

//3
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    insetForSectionAt section: Int) -> UIEdgeInsets {
    return sectionInsets
}

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

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return sectionInsets.left

}

How I can scroll each cell to center perfectly? Here is how it shows my collection on start, enter image description here

Thats what it shows on scroll, it get cut from left side enter image description here

Upvotes: 0

Views: 1267

Answers (1)

Gurpreet Singh
Gurpreet Singh

Reputation: 41

Don't use static height and width. call this function.Maybe its helpful for you:-

// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // return CGSize(width: yourCollectionView.frame.size.width, height: yourCollectionView.frame.size.height ) // }

Upvotes: 0

Related Questions