2 Replies
      Latest reply on Nov 17, 2019 12:48 AM by Urkman
      Urkman Level 1 Level 1 (0 points)

        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

        • Re: List in a Tabview always reset after tab change
          Urkman Level 1 Level 1 (0 points)

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

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

          • Re: List in a Tabview always reset after tab change
            Jim Dovey Level 3 Level 3 (210 points)

            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."