UICollectionViewCell:AutoLayout - 大小计算不正确(偏移 exacatly 10 pt)

UICollectionViewCell: AutoLayout - incorrect size calculation (offset by exacatly 10 pt)

我有一组 UICollectionViewCell subclass 具有不同布局(使用 AutoLayout)。

由于 "Self Sizing Cells" 功能完全损坏,我在 DataSource 中手动计算大小。但是,我注意到计算出的大小 正好比应有的小 10 pt,与单元格无关。

这是我用来计算尺寸的代码:

  let sizeCache = CellCache() // Size cache stores previously calculated values
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if let cachedSize = sizeCache.sizeAtIndexPath(indexPath: indexPath) {
      return cachedSize // Return cached value if it exists
    }

    // Calculate the size if not found in the cache
    let cellClass = cellTypeAt(indexPath: indexPath) // Get the class type
    let cell = sizeCache.createCellOfTypeIfNeeded(cellType: cellClass) // Dequeue or instantiate a cell
    self.collectionView(collectionView, cell: cell, configureAt: indexPath) // Ask DataSource to configure this cell
    let fittingSize = CGSize(width: collectionView.bounds.width - 16, height: 0) // Set the padding to 16
    cell.bounds = CGRect(origin: .zero, size: fittingSize) // Set cell's width
    var result = cell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) // Calculate cell's size using AutoLayout
    result.width = collectionView.bounds.width - 16 // Set the padding
    result.height = result.height + 10 // !!! Error - Add 10 pt to the result !!!
    sizeCache.setSize(size: result, at: indexPath) // Cache the result
    return result
  }

请注意末尾的行,其中 我必须添加 10 pt 才能使其正常工作。

我的所有单元格都是此基础 class 的子class,它设置了 width 约束:

import UIKit
import SnapKit

class AutoSizingCellBase: UICollectionViewCell {
  override class var requiresConstraintBasedLayout: Bool {
    return true
  }

  private final var widthConstraint: Constraint?

  override func updateConstraints() {
    if widthConstraint == nil {
      if let window = window {
        let width = window.bounds.width - 16
        contentView.snp.makeConstraints { (make) in
          widthConstraint = make.width.equalTo(width).constraint
        }
      }
    }
    super.updateConstraints()
  }
}

问题示例:

正确大小(添加 10 磅后)

大小不正确(未添加 10 pt)

该问题影响所有单元格。其根本原因可能是什么?

更新:约束示例 以下是我用来配置显示的视图的约束:

  private func setupConstraints() {
    price.setContentCompressionResistancePriority(.required, for: .horizontal)
    disclosureIndicator.setContentCompressionResistancePriority(.required, for: .horizontal)
    disclosureIndicator.setContentCompressionResistancePriority(.required, for: .vertical)
    disclosureIndicator.setContentHuggingPriority(.required, for: .horizontal)
    disclosureIndicator.setContentHuggingPriority(.required, for: .vertical)

    title.snp.makeConstraints { (make) in
      make.leading.top.equalTo(contentView.layoutMarginsGuide)
      make.trailing.lessThanOrEqualTo(price.snp.leading).offset(-8)
    }

    subtitle.snp.makeConstraints { (make) in
      make.top.equalTo(title.snp.bottom).offset(8)
      make.leading.bottom.equalTo(contentView.layoutMarginsGuide)
    }

    disclosureIndicator.snp.makeConstraints { (make) in
      make.trailing.equalTo(contentView.layoutMarginsGuide)
      make.centerY.equalToSuperview()
    }

    price.snp.makeConstraints { (make) in
      make.trailing.equalTo(disclosureIndicator.snp.leading).offset(-8)
      make.centerY.equalToSuperview()
    }
  }

这是一个相当复杂的示例,但即使使用更简单的示例也可以重现该问题:

  private func setupConstraints() {
    button.snp.makeConstraints { (make) in
      make.width.equalToSuperview() // Button is a subview of the UIStackView
    }

    stack.snp.makeConstraints { (make) in
      make.edges.equalTo(contentView.layoutMarginsGuide)
      make.height.greaterThanOrEqualTo(150)
    }
  }

更新

在约束中添加偏移量解决了问题,而视图层次调试器仍然显示 "Ambiguous layout":

subtitle.snp.makeConstraints { (make) in
  make.top.equalTo(title.snp.bottom).offset(8)
  make.leading.equalTo(contentView.layoutMarginsGuide)
  make.bottom.equalTo(contentView.layoutMarginsGuide).offset(-10) // Subtracted 10
}

问题是10来自哪里仍然是开放的。

更新 3 似乎部分问题在于调整 layoutMargins:

UIView.appearance().layoutMargins = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)

在我从 AppDelegate 中删除上面的行后,大小计算变得正确,尽管单元格的外观发生了变化。 所以我认为问题是出于某种原因,为调整大小而创建的单元格与从 UICollectionView 出队的单元格具有不同的插图。

LayoutMargins 对于添加到父视图和不在视图层次结构中的 UICollectionViewCell 的计算方式不同。

因为行:

UIView.appearance().layoutMargins = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)

添加到超级视图的单元格与为调整大小而创建的单元格具有不同的边距。因此差异。