Skip to content

Instantly share code, notes, and snippets.

@felipevlima
Created June 21, 2021 01:00
Show Gist options
  • Save felipevlima/c780479b01bbe5fdafa467e950c44fd9 to your computer and use it in GitHub Desktop.
Save felipevlima/c780479b01bbe5fdafa467e950c44fd9 to your computer and use it in GitHub Desktop.
Implementing GridStack on Swift UI
import SwiftUI
struct GridStack<Content: View>: View {
let rows: Int
let columns: Int
let content: (Int, Int) -> Content
var body: some View {
VStack {
ForEach(0..<rows) { row in
HStack {
ForEach(0..<self.columns) { column in
self.content(row, column)
}
}
}
}
}
init (rows: Int, columns: Int, @ViewBuilder content: @escaping(Int, Int) -> Content) {
self.rows = rows
self.columns = columns
self.content = content
}
}
struct ContentView: View {
var body: some View {
GridStack(rows: 4, columns: 4) { row, col in
HStack {
Image(systemName: "\(row * 4 + col).circle")
Text("R\(row) C\(col)")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment