For some reason updating the selection does not update the selected tab in this very simple example. My expectation would be that it would show Tab 1 initially, and when I press the Toggle Tab button, it should slide over to Tab 0. Instead I have to toggle the tab 3 times before it finally slides to Tab 0.
Tested in iOS 16
struct Test: View {
@State var currentTab = 1
var body: some View {
VStack {
Text("Current Tab: \(currentTab)")
TabView(selection: $currentTab) {
Text("Tab 0")
.tag(0)
Text("Tab 1")
.tag(1)
}
.tabViewStyle(.page(indexDisplayMode: .never))
Button {
withAnimation(Animation.easeIn(duration: 0.2)) {
if currentTab == 0 {
currentTab = 1
} else {
currentTab = 0
}
}
} label: {
Text("Toggle Tab")
}
}
}
}