The question might look familiar, but I went through all solutions on this topic but none had a working approach for the latest versions of SwiftUI and iOS.
So here is my tab view, I am trying to animate when switching between the tabs. I tried binding animations by adding animation to the binding and that does not work. I also tried attaching the onChange modifier on the TabView itself which prints the correctly selected tab but it does not animate so neither approach works could someone point to the correct implementation of this?
struct MainTabScreen: View {
@State private var selectedTab = 0
var body: some View {
// The binding animation does not animate
TabView (selection: $selectedTab.animation(
.easeInOut(duration: 1.0))
) {
Home()
.tabItem {
Image(systemName: "house")
Text("Home")
}
.tag(0)
Dollar()
.tabItem {
Image(systemName: "dollarsign.circle")
Text("Dollar")
}
.tag(1)
Menu()
.tabItem {
Image(systemName: "plus")
Text("Menu")
}
.tag(2)
}
.onChange(of: selected, perform: { tab in
print("TAPPED ON :\(tab)") // prints correct tab
withAnimation(.easeInOut(duration: 2)) {
selectedTab = tab // does not animate
}
})
}
}
Most solutions online advise using .animation(.easeInOut)
to the tab view itself, but this is now deprecated. Looking for a solution that works with iOS 15 and Swift 5.