How to create grid in SwiftUI

后端 未结 4 1767
孤街浪徒
孤街浪徒 2020-12-21 09:35

I know that we can create a List in vertical SwiftUI like this,

struct ContentView : View {
    var body: some View {
        NavigationView {
            Li         


        
4条回答
  •  轮回少年
    2020-12-21 09:58

    Available for iOS/iPadOS 14 on Xcode 12. You can use LazyVGrid to load just what the user see into screen and not the whole list, List is lazy by default.

    import SwiftUI
    
    //MARK: - Adaptive
    struct ContentView: View {
        
        var body: some View {
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum:100))]) {
                    ForEach(yourObjects) { object in
                        YourObjectView(item: object)
                    }
                }
            }
        }
    }
    
    //MARK: - Custom Columns
    struct ContentView: View {
            
        var body: some View {
             ScrollView {   
                 LazyVGrid(columns: Array(repeating: GridItem(), count: 4)) {
                     ForEach(yourObjects) { object in
                         YourObjectView(item: object)
                     }
                 }
             }
        }
    }
    

    Don't forget replace the info objects with your info and YourObjectView with your customView.

提交回复
热议问题