Created
June 21, 2021 01:00
-
-
Save felipevlima/c780479b01bbe5fdafa467e950c44fd9 to your computer and use it in GitHub Desktop.
Implementing GridStack on Swift UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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