Search code examples
iosuicollectionviewuicollectionviewcelluicollectionviewlayout

Change the flow layout of a UICollectionView


Based on filter results, I need to change the flow layout of a collection view and reload the data using a different kind of cell. However you can't just do this:

myCollectionView.flowLayout = ...

So how is it possible to change the flow layout of a collection view?


Solution

  • SHORT AND SIMPLE ANSWER

    You can access/set the flowLayout like this:

    collectionView.collectionViewLayout = ...
    

    LONG ANSWER

    You said:

    However you can't just do this:

    myCollectionView.flowLayout = ...
    

    But in fact you can set the collection view’s collectionViewLayout, and it does just what you want:

    Assigning a new layout object to this property causes the new layout to be applied ... to the collection view’s items.