简体   繁体   中英

How to make UICollectionView dynamic height?

How to make UICollectionView dynamic height? The height of the UICollectionView should depend on the number of cells in it.

class ProduitViewController: UIViewController {
    
    var productCollectionViewManager: ProductCollectionViewManager?
    var sizeCollectionViewManager: SizeCollectionViewManager?
    var product: ProductModel?
    var selectedSize: String?
    
    @IBOutlet weak var productCollectionView: UICollectionView!
    @IBOutlet weak var sizeCollectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }

}

private extension ProduitViewController {

    func setup() {
        
        guard let product = product else { return }
        colorNameLabel.text = product.color[0].name
        
        sizeCollectionViewManager = SizeCollectionViewManager.init()
        sizeCollectionView.delegate = sizeCollectionViewManager
        sizeCollectionView.dataSource = sizeCollectionViewManager
        sizeCollectionViewManager?.set(product: product)
        sizeCollectionViewManager?.didSelect = { selectedSize in
            self.selectedSize = selectedSize
        }
        sizeCollectionView.reloadData()
    }
}

Collection View Manager

import UIKit

final class SizeCollectionViewManager: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    var sizeProduct: [SizeModel] = []
    
    var didSelect: ((String) -> Void)?
    
    func set(product: ProductModel) {
        sizeProduct = product.size
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return sizeProduct.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SizeCell", for: indexPath) as? SizeCollectionViewCell {
            
            cell.configureCell(cellModel: sizeProduct[indexPath.row])
            return cell
        }
        
        return UICollectionViewCell.init()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = collectionView.frame.width / 3 + 20
        let height: CGFloat = 35
        
        return CGSize(width: width, height: height)
    }
}

The height is now 35. If it is not set static, then the collection view will disappear altogether from the screen.

Screenshot Storyboard 在此处输入图像描述

You should set a height constraint on the UICollectionView reference. Once the constraint is set, you can calculate and set the constraint value based on number of objects, since you know how many rows it should display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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