How do I create a horizontal scrolling UICollectionView in Swift?

前端 未结 3 666
灰色年华
灰色年华 2020-12-13 07:37

How can I 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

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 08:20

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

    layout.scrollDirection = .horizontal
    

    Here is a nice tutorial for more info: https://www.youtube.com/watch?v=Ko9oNhlTwH0

    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)
    

提交回复
热议问题