如何在swift 4中以分页模式在集合视图中使用中心垂直单元格?

我读了如何垂直居中UICollectionView内容但是当我在这里使用代码时

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { let navBarHeight = self.navigationController?.navigationBar.frame.height let collectionViewHeight = (self.collectionView?.frame.height)! - navBarHeight! let itemsHeight = self.collectionView?.contentSize.height let topInset = ( collectionViewHeight - itemsHeight! ) / 4 return UIEdgeInsetsMake(topInset ,0, 0 , 0) } 

但是当滚动集合视图时,这将显示不正确的forms,因此这里是我的代码,因为我的集合视图单元格是正方形并且等于屏幕宽度

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() showImagesCV.contentInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing) if let flowLayout = showImagesCV.collectionViewLayout as? UICollectionViewFlowLayout{ cellWidth = UIScreen.main.bounds.width cellHeight = cellWidth //yourCellHeight = cellWidth if u want square cell flowLayout.minimumLineSpacing = spacing flowLayout.minimumInteritemSpacing = spacing UIView.performWithoutAnimation { } } } } extension showImagesViewController : UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: cellWidth, height: cellHeight) } } 

我希望单元格之间的间距为0,显示的每个单元格(当前页面(因为集合视图处于分页模式))是屏幕的中心 这是照片中的问题

关于您提到和使用的SO问题 ,即整个部分不是单个单元格的中心。 您的某些代码可能无效(需要重做)。

首先也是最重要的一点,将collectionView的约束设置为全屏。 请记住考虑safeAre / TopGuide,这将确保集合视图位于导航栏下方(如果有)。

这将确保您的collectionView是最新的

  override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() myCollectionView.collectionViewLayout.invalidateLayout() } 

在代码下面转储/注释,并在故事板中的collectionView的检查器中将insets设置为(0,0,0,0)。 在同一个检查器中,将Min Spacing更改为0表示单元格和行,两者都是。

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { let navBarHeight = self.navigationController?.navigationBar.frame.height let collectionViewHeight = (self.collectionView?.frame.height)! - navBarHeight! let itemsHeight = self.collectionView?.contentSize.height let topInset = ( collectionViewHeight - itemsHeight! ) / 4 return UIEdgeInsetsMake(topInset ,0, 0 , 0) } 

同时删除/注释下面的代码

  showImagesCV.contentInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing) if let flowLayout = showImagesCV.collectionViewLayout as? UICollectionViewFlowLayout{ cellWidth = UIScreen.main.bounds.width cellHeight = cellWidth //yourCellHeight = cellWidth if u want square cell flowLayout.minimumLineSpacing = spacing flowLayout.minimumInteritemSpacing = spacing UIView.performWithoutAnimation { } } 

现在改变你的sizeForItemAt就像这样。

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

UICollectionViewCell现在在您的collectionView单元格中,确保您的图像视图为1:1并位于中心。 还处理其他UI组件的约束。

PS:这是简单的方法,其他的就是编写自定义的flowLayout