菜鸟教程小白 发表于 2022-12-11 19:26:52

ios - 如何居中对齐我的 collectionView 单元格?


                                            <p><p>我试过这个解决方案 <a href="https://stackoverflow.com/questions/41091177/swift-3-how-to-center-horizontally-the-last-row-of-cells-in-a-uicollectionview" rel="noreferrer noopener nofollow">here</a>但它似乎只适用于垂直布局。我正在尝试使其适用于水平布局。就我而言,我总是希望顶部有 3 个单元格,底部有 2 个单元格,并且中心对齐。</p>

<p>示例:<img src="/image/PKInZ.png" alt="Example"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为这应该对您有所帮助(我在委托(delegate)方法中定义了插图,因为 collectionview 否则只会将我的第一个部分居中而其他部分保持不变):</p>

<pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -&gt; UIEdgeInsets {

      // handling layout
      // which needs to be centered vertically and horizontally
      // also:
      // maximum number of items = 6
      // maximum number of rows = 2
      // maximum number of items in row = 3
      let numberOfItems: CGFloat
      let numberOfRows: CGFloat
      if collectionView.numberOfItems(inSection: section) &gt; Constants.maxNumberOfItemsInRow{
            numberOfItems = CGFloat(Constants.maxNumberOfItemsInRow)
            numberOfRows = CGFloat(Constants.maxNumberOfRows)
      } else {
            numberOfItems = CGFloat(collectionView.numberOfItems(inSection: section))
            numberOfRows = CGFloat(Constants.minNumberOfRows)
      }


      let totalCellWidth = Constants.itemSize.width * numberOfItems
      let totalSpacingWidth = Constants.minimumInteritemSpacing * numberOfItems
      var leftInset = (collectionView.layer.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2

      let totalCellHeight = Constants.itemSize.height * numberOfRows
      let maximumSectionHeight = (Constants.itemSize.height * CGFloat(Constants.maxNumberOfRows)) + (CGFloat(Constants.maxNumberOfRows + 1) * Constants.minimumLineSpacing)

      if leftInset &lt; 0.0 { leftInset = 0.0 }

      let topInset = (maximumSectionHeight - totalCellHeight) / 2

      let rightInset = leftInset

      return UIEdgeInsets(top: topInset, left: leftInset, bottom: topInset, right: rightInset)
    }
</code></pre>

<p>另外,布局是由类定义的:</p>

<pre><code>class CollectionViewFlowLayout: UICollectionViewFlowLayout {

    // MARK: - Constants

    private struct Constants {
      static let minimumInteritemSpacing: CGFloat = 12.5
      static let minimumLineSpacing: CGFloat = 16.5
      static let itemSize = CGSize(width: 64.0, height: 90.0)
    }

    override func prepare() {

      guard let collectionView = collectionView else { return }

      /// Defining flow layout for collectionview presentation
      itemSize = Constants.itemSize

      headerReferenceSize = CGSize(width: collectionView.dc_width, height: 1)
      scrollDirection = .vertical
      minimumInteritemSpacing = Constants.minimumInteritemSpacing
      minimumLineSpacing = Constants.minimumLineSpacing

      super.prepare()
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何居中对齐我的 collectionView 单元格?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47800679/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47800679/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何居中对齐我的 collectionView 单元格?