UICollectionViewCompositionalLayout 水平,使用估计而不是水平拥抱时不计算内容的宽度 [英] UICollectionViewCompositionalLayout horizontal, not calculating width of content when using estimated and not horizontally hugging

本文介绍了UICollectionViewCompositionalLayout 水平,使用估计而不是水平拥抱时不计算内容的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want the cell to horizontaly Hug its content

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

I am using UICollectionViewCompositionalLayout in horizontal mode and with widthDimension: .estimated(100) which should calculate the content size

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

but the width of cell / group is NOT calculated by the content size at all and is set to the estimated values as it would be an absolute value

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

Even adding a width constraint to the label doesn't affect the cells size

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

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
    }
}

推荐答案

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

To size your cells correctly, change:

let group = NSCollectionLayoutGroup.horizo​​ntal(layoutSize: groupSize, subitem: item, count: 1)

为此:

let group = NSCollectionLayoutGroup.horizo​​ntal(layoutSize: groupSize, subitems: [item])

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

I'm unsure why this change is necessary, as these lines appear to say the same thing. That said, it will work provided your collection view cell specifies the actual cell size by overriding sizeThatFits(_:), preferredLayoutAttributesFitting(_:), using Auto Layout, etc.

就其价值而言,第一个函数签名的文档说:

For what it's worth, the documentation for the first function signature says:

指定一个组,其中 N 个项目沿水平轴大小相同.

Specifies a group that will have N items equally sized along the horizontal axis.

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

The comment for the second function signature makes no such claims about equally sized items. It says:

指定一个将重复项目直到可用的水平空间用完的组.

Specifies a group that will repeat items until available horizontal space is exhausted.

这篇关于UICollectionViewCompositionalLayout 水平,使用估计而不是水平拥抱时不计算内容的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆