List in a Tabview always reset after tab change

In my app I have a TabView with lists in there. When I scroll a list and than change tabs forth and back, the list is always reset and starts from the top...


But this shouldn't be the case... Is this a bug, or what I'm doing wrong?


Here is some example code:


struct ContentView: View {
  var body: some View {
       TabView {
            NavigationView {
                 List {
                      ForEach((1...50), id: \.self) {
                           Text("Row in Tab 1 Number: \($0)")
                      }
                 }
                 .navigationBarTitle("Tab 1")
            }.tabItem {
                 Image(systemName: "bubble.right")
                 Text("Tab 1")
            }.tag(0)

            NavigationView {
                 List {
                      ForEach((1...50), id: \.self) {
                           Text("Row in Tab 2 Number: \($0)")
                      }
                 }
                 .navigationBarTitle("Tab 2")
            }.tabItem {
                 Image(systemName: "bubble.left")
                 Text("Tab 2")
            }.tag(1)
       }.edgesIgnoringSafeArea(.top)
  }
}


Hope someone can help me on this.


Thanks,

Stefan

OK.. This seems to be a known bug as mentioned here:

https://github.com/ryangittings/swiftui-bugs

This is a known issue, and it comes down to the implementation of TabView in SwiftUI. The short version is that SwiftUI really wants to implement everything itself, but for various reasons they piggyback some things on UIKit. In this case they use UITabViewController to manage some parts of the logical ordering and view controller management, but ultimately do the drawing themselves using less resources. Each time you switch tabs it's actually using the same view and swapping out its content, discarding the View (a value type) it used before. When you switch back, that earlier state is gone, and gets recreated from scratch.


The long version is here: https://forums.developer.apple.com/thread/124749


I've also filed a bug report on this, and would encourage everyone to file duplicates: the more duplicates there are, the higher priority is given to this particular bug, and a higher priority generally leads to a quicker fix. My issue is FB7408323, and has the title "SwiftUI TabBar: tabbed content loses view state when switching tabs."



struct TabbarDetailView : View {

    

    @State var activeTab = 0

    @State private var oldSelectedItem = 0

    

    var body : some View {

        

        TabView(selection: $activeTab) {

            

            ActivityVW().tabItem {

                VStack {

                    Image("Home")

                        .renderingMode(.template)

                    Text("Activity")

                }

            }

            .tag(0)

            .onAppear { self.oldSelectedItem = self.activeTab }

            

            LeaderboardView().tabItem {

                VStack {

                    Image("Leaderboard")

                        .renderingMode(.template)

                    Text("Leaderboard")

                }

            }

            .tag(1)

            .onAppear { self.oldSelectedItem = self.activeTab }

            

            CreateView().tabItem {

                VStack {

                    Image("create")

                    //   .renderingMode(.template)

                    // Text("Create")

                }

            }

            .tag(2)

            .onAppear { self.oldSelectedItem = self.activeTab }

            

            AchievementsView().tabItem {

                VStack {

                    //                    if activeTab == 4 {

                    //                        Image("ribbon3selected")

                    //

                    //                    } else {

                    Image( "achievement")

                        .renderingMode(.template)

                    //                    }

                    

                    

                    Text("Achievements")

                }

            }

            .tag(3)

            .onAppear { self.oldSelectedItem = self.activeTab }

            

            ProfileView().tabItem {

                VStack {

                    Image("Ellipse 220")

                    // .renderingMode(.template)

                    Text("Profile")

                }

            }

            .tag(4)

            .onAppear {

                self.oldSelectedItem = self.activeTab }

        }

    }

}
List in a Tabview always reset after tab change
 
 
Q