简体   繁体   中英

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, 在此处输入图像描述

Thats what it shows on scroll, it get cut from left side在此处输入图像描述

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 ) // }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM