Swift UICollectionViewCell的动态高度是否以编程方式具有约束?

Swift UICollectionViewCell的动态高度是否以编程方式具有约束?,swift,uicollectionview,uicollectionviewcell,uicollectionviewflowlayout,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewflowlayout,我从未见过一个主题有这么多不同的答案,就像我为集合视图单元格设置动态高度一样。一些解决方案的代码量难以想象,这进一步促成了一种共识,即UICollectionView是一个集群,因为这可以通过UITableView实现,而无需做任何事情,因为当约束正确链接时,动态单元高度是内置的。但我想更好地处理集合视图。这是一场噩梦 如何在Swift中设置集合视图单元格的高度,使其在不使用一百行代码的情况下,以编程方式动态调整其内容大小(无情节提要) 您可以将以下内容插入到一个空白项目中,如果您愿意,它将干净

我从未见过一个主题有这么多不同的答案,就像我为集合视图单元格设置动态高度一样。一些解决方案的代码量难以想象,这进一步促成了一种共识,即
UICollectionView
是一个集群,因为这可以通过
UITableView
实现,而无需做任何事情,因为当约束正确链接时,动态单元高度是内置的。但我想更好地处理集合视图。这是一场噩梦

如何在Swift中设置集合视图单元格的高度,使其在不使用一百行代码的情况下,以编程方式动态调整其内容大小(无情节提要)

您可以将以下内容插入到一个空白项目中,如果您愿意,它将干净地运行

CollectionViewFlowLayout:

class CollectionViewFlowLayout: UICollectionViewFlowLayout {

    // inits
    override init() {
        super.init()

        config()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // config
    func config() {
        scrollDirection = .vertical
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
    }
}
CollectionViewController

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    // model properties
    let colorArray: [UIColor] = [.red, .green, .blue, .yellow]
    let labelArray: [String] = [
        "sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
        "sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
        "sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
        "sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf"
    ]

    // view did load
    override func viewDidLoad() {
        super.viewDidLoad()

        config()
    }

    // config
    func config() {
        collectionView?.dataSource = self
        collectionView?.delegate = self
        collectionView?.backgroundColor = UIColor.gray
        collectionView?.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
        collectionView?.alwaysBounceVertical = true
        collectionView?.alwaysBounceHorizontal = false
        collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cell1")
    }

    // data source: number of items in section
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colorArray.count
    }

    // data source: cell for item
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CollectionViewCell
        cell.backgroundColor = colorArray[indexPath.item]
        cell.label.text = labelArray[indexPath.item]
        return cell
    }

    // delegate: size for item
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: view.frame.width, height: 200)
    }
}
CollectionViewCell

class CollectionViewCell: UICollectionViewCell {

    // view properties
    var label = UILabel()

    // inits
    override init(frame: CGRect) {
        super.init(frame: frame)

        config()
        addLabel()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // config
    func config() {
        self.translatesAutoresizingMaskIntoConstraints = false
    }

    // add label
    func addLabel() {
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        addSubview(label)
        label.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        label.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        label.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
        label.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        label.sizeToFit()
    }
}
在to post的帮助下,我能够以编程方式创建动态UICollectionViewCell:

设置UICollectionView布局的estimatedItemSize,如下所示:

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)
}
在UICollectionViewCell的contentView中添加uicontrol

self.contentView.translatesAutoresizingMaskIntoConstraints = false

let screenWidth = UIScreen.main.bounds.size.width

contentView.addSubview(msgLabel)

[
    msgLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
    msgLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
    msgLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
    msgLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
    msgLabel.widthAnchor.constraint(equalToConstant: screenWidth - 16)
    ].forEach{ $0.isActive = true }
将UILabel的numberOfLines设置为0