修改底层布局约束时,UICollectionViewCell 子类的大小不正确
UICollectionViewCell subclass incorrect size when modifying underlying layout constraints
我有一个 UICollectionView
,它使用在 xib 中定义的流式布局和动态宽度单元格。 xib 在包含动态标签和静态图像的堆栈视图的所有方面都有约束。这些约束应根据 configureCell(cellData:, padding:)
中提供的边距进行更改,它在 cellForItemAt
和 sizeForItemAtIndexPath
datasource/delegate 方法中调用。不幸的是,如果我将约束更改为 configureCell(cellData:, padding:)
中的其他内容,systemLayoutSizeFitting()
返回的大小不正确。
我尝试了 setNeedsLayout()
、layoutIfNeeded()
、setNeedsUpdateConstraints()
、updateConstraintsIfNeeded()
我能想到的所有组合。我也试过 these solutions 没有成功。
如何在 UICollectionViewCell
中更新约束并使其在 collectionView(collectionView:, layout:, sizeForItemAtIndexPath:)
中正确调整大小?
UICollectionViewDataSource
:
private var _sizingViewCell: LabelCollectionViewCell!
//registers and init's sizing cell...
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
_sizingViewCell.configureCell(self.cellData[indexPath.row], padding: defaultItemPadding)
var size = _sizingViewCell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
return size
}
UICollectionViewCell
子类:
func configureCell(_ cellData: LabelCollectionCellData, padding: UIEdgeInsets? = nil) {
removeButton.isHidden = cellData.removable
if let padding = padding {
leadingPaddingConstraint.constant = padding.left
topPaddingConstraint.constant = padding.top
trailingPaddingConstraint.constant = padding.right
bottomPaddingConstraint.constant = padding.bottom
contentStackView.spacing = padding.left
}
}
问题原来是操作失误。
我给 configureCell(cellData:, padding:)
一个不同的填充:
collectionView(_ collectionView:, cellForItemAt indexPath:)
比我放弃的要多:
collectionView(collectionView:, layout collectionViewLayout:, sizeForItemAtIndexPath indexPath:)
导致视图无法正确显示。
我有一个 UICollectionView
,它使用在 xib 中定义的流式布局和动态宽度单元格。 xib 在包含动态标签和静态图像的堆栈视图的所有方面都有约束。这些约束应根据 configureCell(cellData:, padding:)
中提供的边距进行更改,它在 cellForItemAt
和 sizeForItemAtIndexPath
datasource/delegate 方法中调用。不幸的是,如果我将约束更改为 configureCell(cellData:, padding:)
中的其他内容,systemLayoutSizeFitting()
返回的大小不正确。
我尝试了 setNeedsLayout()
、layoutIfNeeded()
、setNeedsUpdateConstraints()
、updateConstraintsIfNeeded()
我能想到的所有组合。我也试过 these solutions 没有成功。
如何在 UICollectionViewCell
中更新约束并使其在 collectionView(collectionView:, layout:, sizeForItemAtIndexPath:)
中正确调整大小?
UICollectionViewDataSource
:
private var _sizingViewCell: LabelCollectionViewCell!
//registers and init's sizing cell...
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
_sizingViewCell.configureCell(self.cellData[indexPath.row], padding: defaultItemPadding)
var size = _sizingViewCell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
return size
}
UICollectionViewCell
子类:
func configureCell(_ cellData: LabelCollectionCellData, padding: UIEdgeInsets? = nil) {
removeButton.isHidden = cellData.removable
if let padding = padding {
leadingPaddingConstraint.constant = padding.left
topPaddingConstraint.constant = padding.top
trailingPaddingConstraint.constant = padding.right
bottomPaddingConstraint.constant = padding.bottom
contentStackView.spacing = padding.left
}
}
问题原来是操作失误。
我给 configureCell(cellData:, padding:)
一个不同的填充:
collectionView(_ collectionView:, cellForItemAt indexPath:)
比我放弃的要多:
collectionView(collectionView:, layout collectionViewLayout:, sizeForItemAtIndexPath indexPath:)
导致视图无法正确显示。