How to return VStack or HStack in SwiftUI

Issue #613

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct VOrH<Content>: View where Content: View {
let isVertical: Bool
let content: () -> Content

init(isVertical: Bool, @ViewBuilder content: @escaping () -> Content) {
self.isVertical = isVertical
self.content = content
}

var body: some View {
makeContent()
}

private func makeContent() -> some View {
if isVertical {
return VStack(spacing: 0) {
content()
}.eraseToAnyView()
} else {
return HStack(spacing: 0) {
content()
}.eraseToAnyView()
}
}
}

Comments