AutoSizing 单元格:单元格宽度等于 CollectionView [英] AutoSizing cells: cell width equal to the CollectionView

查看:24
本文介绍了AutoSizing 单元格:单元格宽度等于 CollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Autolayout 和 UICollectionView 的 AutoSizing 单元格.

我可以在代码中指定单元初始化的约束:

 func configureCell() {snp.makeConstraints { (make) inmake.width.equalToSuperview()}}

但是,由于尚未将单元格添加到 collectionView,因此应用程序会崩溃.

问题

  1. cell 生命周期的哪个阶段可以添加cellwidth 约束?

  2. 是否有任何默认方法可以使 cellwidth等于collectionViewwidth而不访问实例UIScreenUIWindow`?

编辑这个问题不是重复的,因为它不是关于如何使用 AutoSizing 单元格功能,而是在单元格生命周期的哪个阶段应用约束以实现所需的结果使用 AutoLayout.

解决方案

要实现自调整大小的集合视图单元格,您需要做两件事:

  1. UICollectionViewFlowLayout
  2. 上指定 estimatedItemSize
  3. 在您的单元格上实现 preferredLayoutAttributesFitting(_:)

<小时>

1.在 UICollectionViewFlowLayout

上指定 estimatedItemSize<块引用>

此属性的默认值为 CGSizeZero.将其设置为任何其他值会导致集合视图使用单元格的 preferredLayoutAttributesFitting(_:) 方法查询每个单元格的实际大小.如果所有单元格的高度相同,请使用 itemSize 属性而不是此属性来指定单元格大小.

只是一个估计,用于计算滚动视图的内容大小,将其设置为合理的值.

让 collectionViewFlowLayout = UICollectionViewFlowLayout()collectionViewFlowLayout.estimatedItemSize = CGSize(宽度:collectionView.frame.width,高度:100)

<小时>

2.在你的 UICollectionViewCell 子类

上实现 preferredLayoutAttributesFitting(_:)

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) ->UICollectionViewLayoutAttributes {让 autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)//指定你想要的_full width_让targetSize = CGSize(宽度:layoutAttributes.frame.width,高度:0)//使用自动布局计算大小(高度)让 autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizo​​ntalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)让 autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)//将新尺寸分配给布局属性autoLayoutAttributes.frame = autoLayoutFrame返回自动布局属性}

I'm using AutoSizing cells with Autolayout and UICollectionView.

I can specify constraints in code on cell initialization:

  func configureCell() {
    snp.makeConstraints { (make) in
      make.width.equalToSuperview()
    }
  }

However, the app crashes as the cell hasn't been yet added to the collectionView.

Questions

  1. At which stage of the cell's lifecycle it is possible to add a constraint with cell's width?

  2. Is there any default way of making a cell'swidthequal to the widthof thecollectionViewwithout accessing an instance of UIScreenorUIWindow`?

Edit The question is not duplicate, as it is not about how to use the AutoSizing cells feature, but at which stage of the cell lifecycle to apply constraints to achieve the desired result when working with AutoLayout.

解决方案

To implement self-sizing collection view cells you need to do two things:

  1. Specify estimatedItemSize on UICollectionViewFlowLayout
  2. Implement preferredLayoutAttributesFitting(_:) on your cell


1. Specifying estimatedItemSize on UICollectionViewFlowLayout

The default value of this property is CGSizeZero. Setting it to any other value causes the collection view to query each cell for its actual size using the cell’s preferredLayoutAttributesFitting(_:) method. If all of your cells are the same height, use the itemSize property, instead of this property, to specify the cell size instead.

This is just an estimate which is used to calculate the content size of the scroll view, set it to something sensible.

let collectionViewFlowLayout = UICollectionViewFlowLayout()
collectionViewFlowLayout.estimatedItemSize = CGSize(width: collectionView.frame.width, height: 100)


2. Implement preferredLayoutAttributesFitting(_:) on your UICollectionViewCell subclass

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)

    // Specify you want _full width_
    let targetSize = CGSize(width: layoutAttributes.frame.width, height: 0)

    // Calculate the size (height) using Auto Layout
    let autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)
    let autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)

    // Assign the new size to the layout attributes
    autoLayoutAttributes.frame = autoLayoutFrame
    return autoLayoutAttributes
}

这篇关于AutoSizing 单元格:单元格宽度等于 CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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