🐙
SwiftUI hidden tip - _VariadicViewを使ってtagをindexに応じて割り当てる
fileprivate struct TagAssignment<Content: View>: View {
let tree: _VariadicView.Tree<VariadicViewProxy, Content>
init(@ViewBuilder content: () -> Content) {
self.tree = _VariadicView.Tree(VariadicViewProxy(), content: content)
}
var body: some View {
tree
}
struct VariadicViewProxy: _VariadicView_MultiViewRoot {
@ViewBuilder
func body(children: _VariadicView.Children) -> some View {
ForEach(children.indexed(), id: \.element.id) { a in
a.element
.tag(a.index)
}
}
}
}
TabView(selection: $currentIndex) {
TagAssignment {
Self.makeCarouselItem()
Self.makeCarouselItem()
Self.makeCarouselItem()
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
Discussion