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

标签 ios swift uicollectionview autolayout

我使用的是带有默认 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
    }
}

由于某种原因,我遇到各种自动布局错误,并且尺寸出现错误。我是否错误地子类化了?

最佳答案

当您将卡片添加为 subview 时,您没有使用 super View 自动布局它。那是与细胞有关。只需使用单元格以编程方式添加自动布局即可。

关于ios - UICollectionView 导致单元格自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267991/

相关文章:

ios - 背景 View Controller 中的 AVPlayer 缓冲视频导致内存问题

ios - 检测用户点击通知

ios - 如果我在 Storyboard中为 Collection View 单元手动添加单元格,我该如何调用多个重用标识符?

ios - 检测 UICollectionViewCell 的部分

ios - UICollectionView 单元格 subview 不调整大小

ios - Swift 通过 appdelegate 将 View 添加到前面

ios - Web 查看后退按钮历史记录

ios - 从标签栏 Controller 启动 segue

swift - 类型 'string' 的值没有成员 'text'

ios - swift 。选择时更改自定义 Tableview 单元格标签的颜色