CollectionView与背景图像

我使用带有透明背景的collectionView和CollectionView下的imageView。 我根据中间的单元格更改了ImageView,但是图像显示不正确,如何解决它,它应该如何看起来如下所示

GIF:

它应该如何工作:

在此处输入图像描述

怎么运行的:

在此处输入图像描述

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionViewRecomend.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomPagerViewCell let m = modals[indexPath.item] cell?.ImageView.image = m.image cell?.ImageView.backgroundColor = .white cell?.Name.text = m.name cell?.descriptions.text = m.description cell?.Folowers.text = String(m.folower) cell?.Publish.text = String(m.publish) ImageSlider.image = modals[indexPath.row].image cell?.liking = { (cells) in if cell?.like.tintColor == UIColor(hex: 0xD1D1D1, alpha: 1) { cell?.like.tintColor = UIColor.orange self.favorite(cell: cell ?? UITableViewCell()) } else { cell?.like.tintColor = UIColor(hex: 0xD1D1D1, alpha: 1) self.favorite(cell: cell ?? UITableViewCell()) } } return cell! } 

CollectionViewCell

 class CustomPagerViewCell: UICollectionViewCell { @IBOutlet weak var ImageView: UIImageView! @IBOutlet weak var Name: UILabel! @IBOutlet weak var descriptions: UILabel! @IBOutlet weak var Publish: UILabel! @IBOutlet weak var Folowers: UILabel! @IBOutlet weak var BG: UIView! @IBOutlet weak var like: UIButton! @objc var liking: ((UICollectionViewCell) -> Void)? @IBAction func like(_ sender: Any) { liking?(self) } override func awakeFromNib() { super.awakeFromNib() // Initialization code like.tintColor = UIColor(hex: 0xD1D1D1, alpha: 1) ImageView.contentMode = .scaleAspectFit ImageView.layer.cornerRadius = 35 ImageView.layer.borderWidth = 1 ImageView.layer.borderColor = UIColor(hex: 0xD4D4D4, alpha: 1).cgColor ImageView.layer.masksToBounds = true } 

insetForSectionAt

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { let cellWidth : CGFloat = self.collectionViewMenu.frame.width/1.2 let numberOfCells = floor(self.view.frame.size.width / cellWidth) let edgeInsets = (self.view.frame.size.width - (numberOfCells * cellWidth)) / (numberOfCells + 1) return UIEdgeInsetsMake(collectionViewRecomend.frame.height - 165, edgeInsets, 0, edgeInsets) } 

CollectionViewFlowLayout

 class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout { override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { if let cv = self.collectionView { let cvBounds = cv.bounds let halfWidth = cvBounds.size.width * 0.5; let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth; if let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) { var candidateAttributes : UICollectionViewLayoutAttributes? for attributes in attributesForVisibleCells { // == Skip comparison with non-cell items (headers and footers) == // if attributes.representedElementCategory != UICollectionElementCategory.cell { continue } if let candAttrs = candidateAttributes { let a = attributes.center.x - proposedContentOffsetCenterX let b = candAttrs.center.x - proposedContentOffsetCenterX if fabsf(Float(a)) < fabsf(Float(b)) { candidateAttributes = attributes; } } else { // == First time in the loop == // candidateAttributes = attributes; continue; } } return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y); } } // Fallback return super.targetContentOffset(forProposedContentOffset: proposedContentOffset) } }