Sebastian Faltoni Software Architect. Passionate about Clean Code, Tech, Web, AI, .NET, Python, IOT, Swift, SwiftUI and also Blazor

SwiftUI 2 DataGrid like Component with LazyVGrid

37 sec read

SwiftUI 2 DataGrid with LazyVGrid

In Swift UI 2 presented at WWDC 2020, they added a new Component named LazyVGrid. With it and with the help of a GeometryReader we can easily create a UX that resemble a DataGrid.

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            VStack {
                LazyVGrid(columns: Array(repeating: .init(.fixed(geometry.size.width / 4)), count: 4)) {
                    
                    Text("Code")
                        .foregroundColor(.white)
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
                        .background(Color.gray)
                    Text("Emoji")
                        .foregroundColor(.white)
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
                        .background(Color.gray)
                    Text("Code")
                        .foregroundColor(.white)
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
                        .background(Color.gray)
                    Text("Emoji")
                        .foregroundColor(.white)
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
                        .background(Color.gray)
                }.font(.largeTitle)
                ScrollView {
                    LazyVGrid(columns: Array(repeating: .init(.fixed(geometry.size.width / 4)), count: 4)) {
                        
                        ForEach((0...79), id: \.self) {
                            let codepoint = $0 + 0x1f600
                            let codepointString = String(format: "%02X", codepoint)
                            Text("\(codepointString)")
                            let emoji = String(Character(UnicodeScalar(codepoint)!))
                            Text("\(emoji)")
                        }
                    }.font(.largeTitle)
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewLayout(.device)
            .previewDevice("iPad (7th generation)")
            
    }
}

References

Sebastian Faltoni Software Architect. Passionate about Clean Code, Tech, Web, AI, .NET, Python, IOT, Swift, SwiftUI and also Blazor

A simple extension to use Combine with PromiseKit and…

Because ServiceStack use PromiseKit for it’s async calls on Swift, i made this little extension that convert a PromiseKit request into a Combine publisher....
Sebastian Faltoni
39 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *