'iOS - Change UICollectionView layout to fill rows instead of columns

Is it possible to change the UICollectionView so that it fills the rows instead of the columns ? Here's an image explaining it :

EDIT : It's an horizontal scroll

EDIT 2 : I already fits 3 cells in a row, my problem is that with 4 cells it doesn't fill the first row.

enter image description here



Solution 1:[1]

I had the exact same issue as you. Collectin view is horizontal & none-scrollable, but it was filling columns first. Weirdly though, changing the direction to "Vertical" fixed the issue though!

Solution 2:[2]

This takes me long hours of tries and finally what solved it was to change the scroll direction to vertical

layout.scrollDirection = .vertical //in case of horizontal collection

layout.scrollDirection = .horizontal //in case of vertical collection

Solution 3:[3]

Yes it is possible.

  1. Make sure the the collection view's width is equal to the screen
  2. To have 3 cells on each row, you have to make sure the 3 cells will be able to fit the width of the screen.
  3. Example: get the width of the screen & divided it by 3. Then set that value to the width of each cell. Also, for these three cells to fit in one row, the spacing between cells has to be zero. By doing that you have 3 cells that will fit exactly on the width of the screen for each row. if you want to add space between each cell, just divide the width of the screen by less than 3.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdurrahman
Solution 2 A_Mo
Solution 3 Joshua Geronimo