title: UiCollectionView cell dynamic width
body: <p>I have a <code>UICollectionView</code>. How can I set the cell width = label width programmatically</p>
  
  <p>I have implemented <code>UICollectionViewDelegateFlowLayout</code> and <code>UICollectionViewDataSource</code>.</p>
  
  <pre><code> let musicType = ["Blues", "Klasik Müzik", "Halk Müzikleri", "Hip-hop", "Caz", "Pop", "Rock", " Enstrümantal Müzik", "House", "Rap" , "Slow"]
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return musicType.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: customCellIdentifier, for: indexPath as IndexPath) as! CustomCell
          cell.nameLabel.text = musicType[indexPath.item]
          cell.sizeToFit()
          return cell
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          ?????
      }
  
  
  
  }
  
  class CustomCell: UICollectionViewCell {
      override init(frame: CGRect) {
          super.init(frame: frame)
          setupView()
      }
  
      let nameLabel: UILabel = {
         let lbl = UILabel()
          lbl.text = "deneme Tag"
          lbl.translatesAutoresizingMaskIntoConstraints = false
          return lbl
      }()
  
  
      func setupView(){
          backgroundColor = UIColor.red
          addSubview(nameLabel)
          addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[v0]-10-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
          addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
  
  
  
  }
  </code></pre>