Home > Software engineering >  Adding a full width row to a CollectionView two-column structure
Adding a full width row to a CollectionView two-column structure

Time:09-26

I created such a structure with CollectionView, there are two items in one line. The whole structure should go on like this, everything is fine until here, but I must add (only 1) slide at the top. So I want to place one more full width image in 1 line. What path should I follow?

An image of my app

CodePudding user response:

If the full-width image that you need to display is just a first item of the data set you're displaying, make the collection view return a custom size for it based on its index path. In order to do it, assign a delegate to your collection view and implement the UICollectionViewDelegateFlowLayout protocol, specifically the collectionView(_:layout:sizeForItemAt:) method. Alternatively, if you're targeting iOS 13 , you can do the same with the UICollectionViewCompositionalLayout, but the implementation will be different, in that case I encourage you to read its documentation.

If the item doesn't really belong to the data set, better create a section header with just that item inside.

  • Related