简体   繁体   English

Swift3-UICollectionViewCel的nativeContentSize为(-1,-1)

[英]Swift3 - UICollectionViewCel's intrinsicContentSize is (-1, -1)

When I try to get cell's intrinsicContentSize in function collectionView(_:didSelectItemAt:) , the result is (-1, -1). 当我试图让细胞的intrinsicContentSize在功能上collectionView(_:didSelectItemAt:) ,结果是(-1,-1)。

I am using flow layout with auto layout config of cell's subviews and did not implement collectionView(_:layout:sizeForItemAt:) . 我正在将流布局与单元格的子视图的自动布局配置一起使用,并且未实现collectionView(_:layout:sizeForItemAt:) Does anyone have ideas? 有人有想法吗?

Edit: 编辑:

the auto layout of cell has fixed width and height. 单元格的自动布局具有固定的宽度和高度。 I turned on self-sizing by setting estimatedItemSize and config the collectionView as following: 我通过设置estimatedItemSize并设置collectionView来启用自调整大小,如下所示:

self.collectionView.delegate = self
self.collectionView.dataSource = self
if let layout = self.collectionView.collectionViewLayout as? 
    UICollectionViewFlowLayout {
        layout.estimatedItemSize = CGSize(width: 100, height: 100)
}

And I try to get the cell's intrinsicContentSize in: 我尝试在以下单元格中获取单元格的intrinsicContentSize

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    let size = cell?.intrinsicContentSize
}

which gives me (-1, -1) for size 给我(-1, -1)size

A UICollectionViewCell has no intrinsicContentSize . UICollectionViewCell没有intrinsicContentSize Why do you expect it to have one? 您为什么期望它有一个?

  • If you want to know what size the cell is at this moment, just ask for its bounds.size . 如果您想知道此时的单元格大小,只需询问其bounds.size

  • If you want to know what size the cell would take on if it were sized from inside by the autolayout constraints of its subviews, call systemLayoutSizeFitting(_:) . 如果您想知道如果通过其子视图的自动布局约束从内部对单元进行大小调整,单元将采用什么大小,请调用systemLayoutSizeFitting(_:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM