SwiftUI Collection View

Hi,


I love SwiftUI so far, but I can't find a way to create a collection view (e.g. with flow layout) with it. Only List is available.


Is there some way to create one or will it be added in later betas?


Thanks

Accepted Reply

According to some developers who talked with SwiftUI engineers, there won't be an equivalent to collection views, at least not yet.


However, it's still possible to integrate platform-specific collection views with SwiftUI. Also, there has been huge improvements to collection view data source and layout this year. 🙂

Replies

I thought the same ... i'm interested in the thread.

Maybe SwiftUI is still very very, very, young and does not offer this capability out of the box and in the first iteration we will require to wrap an UICollectionView from UIKit, same as MapView tutorial. The same will apply to other components like PageControl if I'm not wrong, i don't see it.

Probably very soon will start to come out 3rd parties views as libraries. It would be super easy to use them as new, more evoluted, building blocks and than in future Apple will "absorbe" them in newer version of SwiftUI proposing their enhanced variant.

You're right.


Regarding using PageControl with SwiftUI, there is a tutorial on it (Interfacing with UIKit).


However, collection views are such a widely used component accoss all Apple platforms that it would be useful to have it out of the box. I understand it may be much harder to implement than "List", because of how much versatile it can be, but I really hope it will be added in the future.

SwiftUI will not provide collection views or table views. Instead, you will use a List component to render lists of content.


That said, you can of course wrap a UICollectionViewController in a SwiftUI View. However, going that route goes against the grain of what they are trying to accomplish with SwiftUI.

I agree these are not technically "collection views" or "table views".


What I meant and would like to achieve is a grid layout (like in Music's album library, Apple Books' library, etc.) or cascading layout (e.g. Pinterest) natively in SwiftUI.


According to SwiftUI's documentation, a List is a container that presents rows of data arranged in a single column. What I'm looking for is a container that present data arranged in several columns and rows.

According to some developers who talked with SwiftUI engineers, there won't be an equivalent to collection views, at least not yet.


However, it's still possible to integrate platform-specific collection views with SwiftUI. Also, there has been huge improvements to collection view data source and layout this year. 🙂

I created a small libraray that allows you to create a grid that adjusts to the available width. It's called 📱GridStack and available at https://github.com/pietropizzi/GridStack Give it a spin if you feel it's helpful.