集合视图单元格迅速滚动到中心时发出的问题

我在VC中有一个集合视图,我水平滚动了集合视图,我想水平滚动的每个单元格居中,我尝试了一个代码,但是当它滚动时它显示下一个单元格比第二个单元格更左。我的代码,

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,insetforSectionAt section: Int) -> UIEdgeInsets {
    return sectionInsets
}

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

func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
    return sectionInsets.left

}

如何滚动每个单元格使其完美居中? 这是开始显示我的收藏的方式,

集合视图单元格迅速滚动到中心时发出的问题

这就是它在滚动显示的内容,它从左侧被剪切

集合视图单元格迅速滚动到中心时发出的问题

lbanzg99 回答:集合视图单元格迅速滚动到中心时发出的问题

请勿使用静态的高度和宽度。 调用此功能。也许对您有帮助:-

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

本文链接:https://www.f2er.com/3168747.html

大家都在问