UICollectionViewCell 不强制单元格大小 [英] UICollectionViewCell not forcing Cell Size
问题描述
我正在使用
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 400, height: 600)
}
指定我的 CollectionView
的单元格大小.它工作正常,但只要我在 ContentView
内的 UILabel
中添加宽度约束 (UILabel.width == ContentView.width)
单元格,单元格的宽度缩小到 UILabel
的固有宽度.
to specify my CollectionView
's cell sizes. It works fine but as soon as I add a width constraint (UILabel.width == ContentView.width)
to my UILabel
inside the ContentView
of the cell, the cell's width shrinks to the intrinsic width of the UILabel
.
我正在使用 UICollectionViewDelegateFlowLayout
和水平滚动.
I am using UICollectionViewDelegateFlowLayout
and horizontal scrolling.
如何强制 ContentView
坚持我指定的单元格大小并让子视图遵循自动布局约束?
How can I force the ContentView
to stick to the cell size I specified and let the subviews follow the auto layout constraints?
推荐答案
问题是在情节提要中,您的集合视图的 Estimate Size 配置为自动:
The problem is that in the storyboard your collection view's Estimate Size is configured to Automatic:
如果您希望遵守 sizeForItemAt
实现而不是内部约束,则需要将其设置为 None.
You need to set it to None if you want your sizeForItemAt
implementation to be obeyed rather than the internal constraints.
这篇关于UICollectionViewCell 不强制单元格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!