Ios UICollectionView居中分页,用于小于CollectionView';s宽度

Ios UICollectionView居中分页,用于小于CollectionView';s宽度,ios,swift,uicollectionview,uiscrollview,uikit,Ios,Swift,Uicollectionview,Uiscrollview,Uikit,我试图让我的单元格在屏幕宽度的0.85%左右,这样下一个和上一个单元格就会部分显示出来,告诉用户还有更多的单元格 我尝试使用这里的许多解决方案以及collectionView.isPaginEnabled=true,但可见单元格从不居中 我希望显示的主单元格居中,无论其左/右是否有更多单元格 这是错误的行为和我的代码。谢谢你的帮助 视频: 这个问题本身并不是一个完整的解决方案,但我看到了一些可能会有所帮助的方法 实现您自己的自定义集合视图布局,这样您就可以完全控制单元格布局和属性,并且可以创建

我试图让我的单元格在屏幕宽度的0.85%左右,这样下一个和上一个单元格就会部分显示出来,告诉用户还有更多的单元格

我尝试使用这里的许多解决方案以及
collectionView.isPaginEnabled=true
,但可见单元格从不居中

我希望显示的主单元格居中,无论其左/右是否有更多单元格

这是错误的行为和我的代码。谢谢你的帮助

视频:


这个问题本身并不是一个完整的解决方案,但我看到了一些可能会有所帮助的方法

  • 实现您自己的自定义集合视图布局,这样您就可以完全控制单元格布局和属性,并且可以创建逻辑以使单元格居中
  • 将collecview宽度设置为视图控制器宽度的85%,并将其clipToBounds标记为false,最后将单元格宽度设置为与集合视图宽度相同。我没有对它进行测试,但它可能会显示出列中的le left和right单元格溢出集合视图的边界,因为集合视图的clipToBounds设置为false。如果需要在单元格之间留一个间距,那么实际上可以将每个单元格的视觉内容包装在一个视图中,该视图的前导和尾随之间有一定的间距,结果可能是相同的
  •  override func viewDidLoad() {
        super.viewDidLoad()
        
        // View preparation
        view.addSubview(mainTabBarView)
        mainTabBarView.configure()
    
        // Collection view
        mainTabBarView.collectionView.dataSource = self
        mainTabBarView.collectionView.delegate = self
      }
    
    extension MainTabBarController: UICollectionViewDataSource {
      func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
      }
      
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cells.previewCell, for: indexPath) as! PreviewCell
        cell.configure()
        return cell
      }
    }
    
    extension MainTabBarController: UICollectionViewDelegate {
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width*0.85, height: collectionView.frame.size.height)
      }
    }
    
    extension MainTabBarController: UICollectionViewDelegateFlowLayout {
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
      }
      
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
      }
    }
    
    class MainTabBarView {
      // MARK:- Main configuration
      func configure() {
        ...
        setupCollectionView()
        ...
      }
    
      private func setupCollectionView() {
        let flowLayout = UICollectionViewFlowLayout()
        let screenWidth = UIScreen.main.bounds.width
        flowLayout.itemSize = CGSize(width: Int(screenWidth * 0.85), height: 68)
        flowLayout.minimumInteritemSpacing = 15
        flowLayout.minimumLineSpacing = 15
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRect(), collectionViewLayout: flowLayout)
        collectionView.backgroundColor = Colors.Primary.clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(PreviewCell.self, forCellWithReuseIdentifier: Cells.previewCell)
        collectionView.allowsSelection = false
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        collectionView.isPagingEnabled = true
      }
    }