0

I'm working on a rebuild of UI on mobile App. I've changed my tableView by CollectionView (for have two columns of cells)

I've achieve the work to have dynamic heigh on cell and that work perfectly, as you can see (iOS left and Android right) I'm trying to achieve the android UI with iOS.

Ios and android version

Currently that what I have follow to achieve that : https://www.vadimbulavin.com/collection-view-cells-self-sizing/

I've trying to set fixed width but that's automatically shape to the content.

I think that probably a little thing but I havent work from long time on iOS, so I'm looking for explain to achieve the fixed width size

The controller contain only that :

@IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!{
    didSet {

        self.collectionViewFlowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    }
}

I havent do the 'max width' part of tutorial. All label inside cell have left right constraint fixed, 0 lines and word wrap line break

Thank !

Have a nice day

Benjamin

Benjamin
  • 401
  • 1
  • 3
  • 16

2 Answers2

0

Well, as i see from screenshot, you already have auto height on each cell.

Set fixed width for CollectionViewCell:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    var height:CGFloat = 0.0
    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        height = layout.itemSize.height
    }
    let screenSize = UIScreen.main

    let wi = screenSize.bounds.size.width/2 // can calculate gap between two columns 

    return CGSize(width: wi, height: height)

}
TomSawyer
  • 3,711
  • 6
  • 44
  • 79
  • It's look like the width are not fixed with this, I've line with 1 or 2 columns and different width at every cell – Benjamin Jan 09 '20 at 09:28
0

You need to do a custom layout of class UICollectionViewLayout and set it to your collection view. There are a lot of Pinterest like collection view tutorials. You can check this

Vasilis D.
  • 1,416
  • 13
  • 21