UICollectionView and SwiftUI?

后端 未结 16 1469
时光说笑
时光说笑 2020-12-02 07:32

How to create grid of square items (for example like in iOS Photo Library) with SwiftUI?

I tried this approach but it doesn\'t work:

var body: some          


        
16条回答
  •  情话喂你
    2020-12-02 07:50

    We've developed a swift package that provides a fully featured CollectionView for use in SwiftUI.

    Find it here: https://github.com/apptekstudios/ASCollectionView

    It's designed to be easy to use, but can also make full use of the new UICollectionViewCompositionalLayout for more complex layouts. It supports auto-sizing of cells.

    To achieve a grid view you could use it as follows:

    import SwiftUI
    import ASCollectionView
    
    struct ExampleView: View {
        @State var dataExample = (0 ..< 21).map { $0 }
    
        var body: some View
        {
            ASCollectionView(data: dataExample, dataID: \.self) { item, _ in
                Color.blue
                    .overlay(Text("\(item)"))
            }
            .layout {
                .grid(layoutMode: .adaptive(withMinItemSize: 100),
                      itemSpacing: 5,
                      lineSpacing: 5,
                      itemSize: .absolute(50))
            }
        }
    }
    

    See the demo project for examples of far more complex layouts.

提交回复
热议问题