我目前正在学习SwiftUI,对于诸如List,Form,HStack,VStack等容器视图如何在后台修改和布局由ViewBuilder构造的视图,我感到困惑。
让我们说我想创建自己的SegmentedControl,它接受选择和内容。如何从ViewBuilder返回的内容中为选定的细分和未选定的细分设置样式?
struct SegmentedControl<Content: View>: View {
private let content: () -> Content
private let selection: Binding<Int>
init(selection: Binding<Int>, @ViewBuilder content: @escaping () -> Content) {
self.content = content
self.selection = selection
}
var body: some View {
content() // <- How do I proceed from here?
}
}