Search code examples
iosuicollectionviewuicollectionviewlayout

How do I change the horizontal spacing between cells in a horizontal scroll collection view


I have a collection view which is basically a horizontal table view. It's one line of cells that scrolls left and right. Sort of like a carousel without the fancy animation. I would like to be able to control the distance between cells. There is a property for doing that in the interface builde. It is in the collection view flow layout, in the inspector (in the tab with the little ruler where you usually see constraints). It is called Min. Spacing and has two variants: For Cells and For Lines. I changed the value of For Cells but it doesn't affect the spacing. Neither does changing the parameter minimumInteritemSpacing or using the collection view callback for specifying that parameter on a per section basis. Help!


Solution

  • The answer is that in a horizontal scroll collection view, the distance between cells, even the horizontal distance, seems to be controlled by the For Lines parameter (in IB) or minimumLineSpacing parameter of the CollectionViewFlowLayout.

    This answer is also buried in some answers I saw more general question about cell spacing in collection views: https://stackoverflow.com/a/48791318/826946 and https://stackoverflow.com/a/37420529/826946

    I decided to separate it out into a separate question that directly addresses the issue of horitzontal scroll collection views.