user6520705 user6520705 - 4 months ago 37
Swift Question

Creating a horizontal scrolling collectionview in Swift

Any one know how I can easily make a horizontal scrolling collectionView, that fills up Cells going across the rows rather than down the columns. I want there to 5 columns and 3 rows but when there is more than 15 items I want it to scroll to the next page. Im having alot of trouble getting this going. Right now i have the collection set up but the cells are filling up down the columns and the spacing between rows wont adjust for me using

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0 }

enter image description here


Where you have a reference to your UICollectionViewFlowLayout(), just do:

layout.scrollDirection = .horizontal

Here is a nice tutorial for more info:

Though for historical purposes, consider searching StackOverFlow quickly to make sure this isn't a duplicate.

Hope this helps.

Update: Your items will fill horizontally first and if there is not enough room within the collectionview going to the right, they will go to next row. So, start by increasing your collectionview.contentsize (should be larger the screen to enable scrolling) and then set your collectionview item (cell) size.

flowLayout.itemSize = CGSize(width: collectionView.contentSize.width/5, height: collectionView.contentSize.height/3)