How to create horizontally dynamic UICollectionView cells? Swift

后端 未结 2 1872
忘掉有多难
忘掉有多难 2021-01-22 00:03

Hey I\'m trying display a set of \"tags\" in a view controller using collection view cells but I\'m having trouble finding a way to make them be able to dynamically resizable de

2条回答
  •  梦谈多话
    2021-01-22 00:26

    You need unambiguous constraints between your label and the cell (e.g. leading, trailing, top, and bottom constraints):

    Then you can use UICollectionViewFlowLayoutAutomaticSize for the itemSize of your collectionViewLayout. Don't forget to set estimatedItemSize, too, which enables automatically resizing cells:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = UICollectionViewFlowLayoutAutomaticSize
        layout.estimatedItemSize = CGSize(width: 100, height: 40)
    }
    

    That yields:

提交回复
热议问题