0

I have a collection view whose cell widths need to be set automatically based on the label size. I have tried many approaches but none of them seem to work for me.

This is what is looks like originally: https://ibb.co/Nn0BtyG

This is what i'm going for: https://ibb.co/ckKk9R5

This is what it looks like after I add the autoresizing code: https://ibb.co/vLSyCD6

The autoresizing code:

    // Layout
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    if #available(iOS 10.0, *) {
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    } else {
        layout.estimatedItemSize = CGSize(width: self.tagsCollectionView.bounds.width, height: 50)
    }
    self.tagsCollectionView.collectionViewLayout = layout

Here's my entire code:

import UIKit

class TagsCollection: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate{

let reuseIdentifier = "TagCell" // also enter this string as the cell identifier in the storyboard
var arrayOfTags: [String] = Array()
var phassetID : String!

func displayTags(){
    do{
        let assetId = phassetID
        let allImagesTagsData = try [Images]()
        let assetIndex = allImagesTagsData.firstIndex(where: { $0.id == assetId })

        if assetIndex != nil{
            arrayOfTags = Array(allImagesTagsData[assetIndex!].tags)
        }
    }catch{
    print("Tag Display View Error: \(error)")
    }
}

func setAssetID(assetID: String){
    phassetID = assetID
}

override func awakeFromNib() {
    self.dataSource = self
    self.delegate = self

}

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    displayTags()
    return arrayOfTags.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! TagCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = arrayOfTags[indexPath.item]
    cell.backgroundColor = UIColor.cyan // make cell more visible in our example project

    return cell
}

// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}
    }

As you can see I have not used the sizeforItem at delegate to set the size of the cells anywhere. Any ideas? Thanks

Samir K
  • 91
  • 2
  • 11
  • The Links you provided are not working – JhonnyTawk Jan 29 '19 at 21:40
  • Whoops! Thank you for pointing that out @JhonnyTawk. Working now – Samir K Jan 29 '19 at 22:41
  • 1
    You can try reviewing this [SO answer] (https://stackoverflow.com/questions/24608568/fixed-spacing-between-item-in-uicollectionview-with-flow-layout) or this [blog post](https://www.codentrick.com/create-a-tag-flow-layout-with-uicollectionview/) – Dennis W. Jan 29 '19 at 23:37

0 Answers0