I'm trying to keep track of what page the user is on in a TabView
that is PageTabViewStyle
in SwiftUI but I can't figure out the best way to keep track of the page index? Using .onAppear
doesn't work well as it gets called multiple times and pages 2 and 3 get called even when not on the screen.
@State var pageIndex = 0
var body: some View {
VStack {
Text("current page = \(0) ")
TabView {
Text("First")
.onAppear {
pageIndex = 0
}
Text("Second")
.onAppear {
pageIndex = 1
}
Text("Third")
.onAppear {
pageIndex = 2
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
}