【问题标题】:UICollectionViewCompositionalLayout horizontal, not calculating width of content when using estimated and not horizontally huggingUICollectionViewCompositionalLayout 水平,使用估计和不水平拥抱时不计算内容的宽度
【发布时间】:2020-04-15 11:10:02
【问题描述】:
I want the cell to horizontaly Hug its content

我在水平模式下使用UICollectionViewCompositionalLayout 并使用widthDimension: .estimated(100) 应该计算内容大小

但单元格/组的宽度根本不是由内容大小计算的 并设置为估计值,因为它是一个绝对值

即使为标签添加宽度约束也不会影响单元格大小

这是预期的行为吗?如果是,那为什么?以及如何得到想要的结果?

class LabelCell: UICollectionViewCell {
    @IBOutlet weak var label: UILabel!
}

class ViewController: UIViewController, UICollectionViewDataSource {

    func buildLayout() -> UICollectionViewCompositionalLayout {

        let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(100), heightDimension: .absolute(32))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(100), heightDimension: .absolute(32))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 1)

        let section = NSCollectionLayoutSection(group: group)
        section.interGroupSpacing = 10

        let configuration = UICollectionViewCompositionalLayoutConfiguration()
        configuration.scrollDirection = .horizontal
        return UICollectionViewCompositionalLayout(section: section, configuration: configuration)

    }

    @IBOutlet weak var collectionView: UICollectionView!

    let items = ["1", "12", "12345", "123456789"]

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.collectionViewLayout = buildLayout()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let c = collectionView.dequeueReusableCell(withReuseIdentifier: "LabelCell", for: indexPath) as! LabelCell
        c.label.text = items[indexPath.row]
        return c
    }
}

【问题讨论】:

  • 尝试给出像 fractionalHeight(1) 和 fractionalWidth(1) 这样的项目的高度和宽度,这可能是布局的问题。

标签: swift cocoa-touch uicollectionview uicollectionviewlayout uicollectionviewcompositionallayout


【解决方案1】:

要正确调整单元格的大小,请更改:

let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 1)

到这里:

let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

我不确定为什么需要进行此更改,因为这些行似乎在说同样的事情。也就是说,只要您的集合视图单元格通过覆盖sizeThatFits(_:)preferredLayoutAttributesFitting(_:)、使用自动布局等来指定实际单元格大小,它将起作用。

对于它的价值,第一个函数签名的文档说:

指定一个组,该组将在水平轴上有 N 个大小相同的项目。

第二个函数签名的注释没有对同样大小的项目做出这样的声明。它说:

指定将重复项目直到可用水平空间耗尽的组。

【讨论】:

  • 是的,使用subitems: [item] 它表现得很好!...但即使给出文档“指定一个组,该组将具有沿水平轴大小相同的 N 个项目。” N == 1,所以我不明白为什么所有项目的大小都应该相等...看起来每次使用 count: 变体时,无论 N 多少,它们的大小都是相等的
  • 我同意。这感觉像是出乎意料的行为。我可能会向 Apple 提交错误报告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2012-07-13
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多