Skip to content

Commit 1585c43

Browse files
committed
Add simple implementation of Grid
1 parent e0569bd commit 1585c43

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// A view that arranges subviews in a flexible grid layout.
2+
public struct Grid<Content: View>: View {
3+
let alignment: Alignment
4+
let horizontalSpacing: Int?
5+
let verticalSpacing: Int?
6+
let content: () -> Content
7+
8+
public init(
9+
alignment: Alignment = .center,
10+
horizontalSpacing: Int? = nil,
11+
verticalSpacing: Int? = nil,
12+
@ViewBuilder content: @escaping () -> Content
13+
) {
14+
self.alignment = alignment
15+
self.horizontalSpacing = horizontalSpacing
16+
self.verticalSpacing = verticalSpacing
17+
self.content = content
18+
}
19+
20+
public var body: some View {
21+
VStack(alignment: .leading, spacing: verticalSpacing) {
22+
content()
23+
}
24+
}
25+
}
26+
27+
/// A single row within a Grid.
28+
public struct GridRow<Content: View>: View {
29+
let alignment: VerticalAlignment
30+
let content: () -> Content
31+
32+
public init(
33+
alignment: VerticalAlignment = .center,
34+
@ViewBuilder content: @escaping () -> Content
35+
) {
36+
self.alignment = alignment
37+
self.content = content
38+
}
39+
40+
public var body: some View {
41+
HStack(alignment: alignment) {
42+
content()
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)