1

The horizontal scrolling in UICollectionView only returns one row and I need to make a horizontal scrolling collection view with 2 rows just like the image below

[1]: https://i.stack.imgur.com/Vs1kt.png :[1]

imgroot
  • 131
  • 3
  • 11

1 Answers1

5

You need to set fix height of the CollectionView and then use sizeForItemAt() under UICollectionViewDelegateFlowLayout which returns CGSize. You have to manage the cell height something like this.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            return CGSize(width: 50.0, height: collectionViewHeight / 2) //<-- Manage height and width accordingly.
        }
Nikunj Rajyaguru
  • 196
  • 1
  • 11
  • and how the second row will appear ? coz sizeforitemAt will only manage size . – imgroot Mar 01 '19 at 12:20
  • 1
    Set scroll direction to horizontal and fix height of CollectionView. Now use the above method. Don't forget to Add UICollectionViewDelegateFlowLayout protocol to your ViewController. – Nikunj Rajyaguru Mar 04 '19 at 05:51
  • should i increase the height for collectionview so that it can accommodate two cells ? – imgroot Mar 04 '19 at 06:42
  • Yes, you can manage accordingly also refer : https://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview for cell spacing. – Nikunj Rajyaguru Mar 04 '19 at 07:13