In iOS 12, when does the UICollectionView layout cells, use autolayout in nib

后端 未结 7 1689
一整个雨季
一整个雨季 2020-11-28 03:08

Same code like this

collectionLayout.estimatedItemSize = CGSize(width: 50, height: 25)
collectionLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
co         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 03:17

    The problem is that the feature being posited here — collection view cells that size themselves based on their internal constraints in a UICollectionViewFlowLayout — does not exist. It has never existed. Apple claims that it does, but it doesn't. I have filed a bug on this every year since collection views were introduced and this claim was first made; and my bug reports have never been closed, because the bug is real. There is no such thing as self-sizing collection view cells.

    See also my answer here: https://stackoverflow.com/a/51585910/341994

    In some years, trying to use self-sizing cells has crashed. In other years, it doesn't crash but it gets the layout wrong. But it doesn't work.

    The only way to do this sort of thing is to implement the delegate method sizeForItemAt and supply the size yourself. You can easily do that by calling

    cell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    

    on a model cell that you have configured in advance. That is what the runtime should be doing for you — but it doesn't.

    So here's my solution to the original question. Instead of a simple array of strings, we have generated an array of string-size pairs (as a tuple). Then:

    override func collectionView(_ collectionView: UICollectionView, 
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCell
            cell.label.text = self.array[indexPath.row].0
            return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        sizeForItemAt indexPath: IndexPath) -> CGSize {
            return self.array[indexPath.row].1
    }
    

提交回复
热议问题