How do you determine spacing between cells in UICollectionView flowLayout

后端 未结 12 1221
夕颜
夕颜 2020-11-29 15:32

I have a UICollectionView with a flow layout and each cell is a square. How do I determine the spacing between each cells on each row? I can\'t seem to find the appropriate

12条回答
  •  醉话见心
    2020-11-29 15:59

    You can do it in two ways.

    Firstly, do some modification in layoutAttributesForItem,

    get the current attributes's layout via the previous layoutAttributesForItem(at: IndexPath(item: indexPath.item - 1, section: indexPath.section))?.frame.

    layoutAttributesForItem(at:): This method provides on demand layout information to the collection view. You need to override it and return the layout attributes for the item at the requested indexPath.

    Secondly, new some attributes via UICollectionViewLayoutAttributes(forCellWith: indexPath),layout them wherever you want.

    And the math is a little larger, because it performs the heavy lifting of the layout.

    layoutAttributesForElements(in:): In this method you need to return the layout attributes for all the items inside the given rectangle. You return the attributes to the collection view as an array of UICollectionViewLayoutAttributes.


    You can check My repo

    • you can put items left aligned

    • you can put items right aligned

    • you can put items right aligned and items reversed

提交回复
热议问题