LazyVStack not refreshing offset of child list in SwiftUI

If initially content/list is large and scrolled to bottom, second time scroll bar offset remains at same place for small reloaded content making contents invisible(as small contents are at top of list). Only after scrolling list to the top small list contents becomes visible else its hidden.

If I replace LazyVStack to VStack, issue goes aways but I won't be able to make sticky sections in it.

To Reproduce this issue:

  1. Click Click me button to reload long list
  2. Scroll to bottom
  3. Click Click me button to reload small list
  4. Small list contents are invisible
  5. Now scroll to top and then it shows small list and adjusts list content size

Output:

//  ContentView.swift
//  LazyVStackIssue
//  Created by Vishwanath Deshmukh on 12/24/21.

import SwiftUI

struct ContentView: View {
    @State var array = ["Vish"]
    @State var count = 1

    var body: some View {

        ZStack(alignment: .top) {
            VStack(spacing: 0) {

                Button(action: refreshData) {
                    Text("Click me")
                }

                ScrollView(.vertical) {

                    LazyVStack(spacing: 0, pinnedViews: [.sectionHeaders]) {

                        VStack(spacing: 20) {

                            // Dynamic Optional section
                            Section(header: Text("Header"), content: {
                                Text("Contents..")
                            })

                            // Dynamic view 2
                            VStack(spacing: 10) {

                                ForEach(array, id: \.self) { itemDataModel in
                                    Text(itemDataModel)
                                        .background(Color.red)
                                        .frame(height: 50)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private func refreshData() {
        count += 1

        if count % 2 == 0 {

            array = ["Vish1", "Vish2", "Vish3", "Vish4", "Vish5", "Vish6", "Vish7", "Vish8", "Vish9", "Vish10", "Vish11", "Vish12", "Vish13", "Vish14", "Vish111", "Vish211", "Vish113", "Vish114", "Vish115", "Vish116", "Vish117", "Vish118", "Vish119", "Vish1110", "Vish1111", "Vish1112", "Vish1113", "Vish1114"  ]

        } else {
            array = ["Vish11", "Vish12", "Vish13", "Vish14", "Vish15"]
        }
    }

}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
LazyVStack not refreshing offset of child list in SwiftUI
 
 
Q