自动调整单元格大小:单元格宽度等于CollectionView [英] AutoSizing cells: cell width equal to the CollectionView

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

问题描述

我正在将AutoSizing单元与Autolayout和UICollectionView一起使用.

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

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

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

问题

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

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

是否有任何默认方法来制作cell width equal to the width of the collectionView without accessing an instance of UIScreen or UIWindow`?

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

修改 问题不是重复的,因为它不是关于如何使用自动调整大小"单元功能的问题,而是在单元生命周期的哪个阶段应用约束以何时使用AutoLayout来获得所需结果的问题.

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. UICollectionViewFlowLayout上指定estimatedItemSize
  2. 在单元格上执行preferredLayoutAttributesFitting(_:)
  1. Specify estimatedItemSize on UICollectionViewFlowLayout
  2. Implement preferredLayoutAttributesFitting(_:) on your cell


1.在UICollectionViewFlowLayout

上指定estimatedItemSize


1. Specifying estimatedItemSize on UICollectionViewFlowLayout

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

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.在UICollectionViewCell子类上实现preferredLayoutAttributesFitting(_:)


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
}

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

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