UICollectionView 导致单元格的自动布局问题

Posted

技术标签:

【中文标题】UICollectionView 导致单元格的自动布局问题【英文标题】:UICollectionView causes autolayout issues with cells 【发布时间】:2017-05-30 17:01:25 【问题描述】:

我正在使用带有默认 UICollectionViewFlowLayout 的 UICollectionView:

let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
cv = UICollectionView(frame: .zero, collectionViewLayout: layout)

我的自定义单元格是:

class CVLegislationCell : UICollectionViewCell 

    let card = CardView() // A view that uses auto layout

    override init(frame: CGRect) 
        super.init(frame: frame)
        setup()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
        setup()
    

    func setup()
    
        self.contentView.addSubview(card)
        card.constrain_edges(to: contentView) // convenience function I wrote for auto layout
    

由于某种原因,我得到了各种自动布局错误,并且尺寸错误。我的子类化不正确吗?

【问题讨论】:

您需要将layout.estimatedItemSize设置为实际值 【参考方案1】:

当您将卡片添加为子视图时,您没有使用超级视图自动布局。那是与细胞。只需通过单元格以编程方式添加自动布局。

【讨论】:

我不太明白你在说什么

以上是关于UICollectionView 导致单元格的自动布局问题的主要内容,如果未能解决你的问题,请参考以下文章