UICollectionView and SwiftUI?

后端 未结 16 1477
时光说笑
时光说笑 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 08:05

    QGrid is a small library I've created that uses the same approach as SwiftUI's List view, by computing its cells on demand from an underlying collection of identified data:

    In its simplest form, QGrid can be used with just this 1 line of code within the body of your View, assuming you already have a custom cell view:

    struct PeopleView: View {
      var body: some View {
        QGrid(Storage.people, columns: 3) { GridCell(person: $0) }
      }
    }   
    
    struct GridCell: View {
      var person: Person
      var body: some View {
        VStack() {
          Image(person.imageName).resizable().scaledToFit()
          Text(person.firstName).font(.headline).color(.white)
          Text(person.lastName).font(.headline).color(.white)
        }
      }
    }
    


    You can also customize the default layout configuration:

    struct PeopleView: View {
      var body: some View {
        QGrid(Storage.people,
              columns: 3,
              columnsInLandscape: 4,
              vSpacing: 50,
              hSpacing: 20,
              vPadding: 100,
              hPadding: 20) { person in
                GridCell(person: person)
        }
      }
    } 
    

    Please refer to demo GIF and test app within GitHub repo:

    https://github.com/Q-Mobile/QGrid

提交回复
热议问题