Fill UICollectionView from left to right when scroll direction is horizontal

When the scroll direction is horizontal, cells are added to the UICollectionView from top to bottom enter image description here

To scroll = vertically, they are added from left to right. enter image description here

The question is, is there a way to add cells from left to right when the scroll direction is horizontal?

+5
source share
3 answers

The order is automatically created in the scroll direction, which is set by default. figure

What you can do is add a custom layout for the UICollectionView using the layoutAttributesForItemAtIndexPath method to set the frame for each individual cell.

 -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes* attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; CGRect cellFrame = CGRectMake(/* calculate your origin x */, /* calculate your origin y */, /* calculate your width */, /* calculate your height */); attr.frame = cellFrame; return attr; } 

More information can be found here.

+1
source

You need to use UICollectionViewFlowLayout and set the scrollDirection property to

 flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 

The default value is vertical (top to bottom)

-1
source

Each type of collection has a layout property that allows you to change the scroll direction. You can do it:

 collectionView.collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 
-1
source

All Articles