I create a simple vertical list that contain items to show.
The problem is when I insert items at the top of the list via insertAtTop()
. The scroll view does not stay in the same place.
See the video: https://streamable.com/i7ywab
How can I make the scroll view stay in the same position?
Here is the sample code.
struct TestView: View {
@State private var items: [Item] = []
var body: some View {
NavigationView {
ScrollView {
LazyVStack {
ForEach(items, id: \._id) { item in
Text(item.text)
.background(Color.green)
.padding(.bottom, 10)
}
}
}
.navigationBarItems(trailing: HStack {
Button("[Insert at top]", action: {
insertAtTop()
})
Spacer(minLength: 20)
Button("[Load]", action: {
load()
})
})
.navigationTitle("Item List")
}
}
private func load() {
items = (1...50).map { _ in Item() }
}
private func insertAtTop() {
let newItems = (1...20).map { _ in Item() }
items.insert(contentsOf: newItems, at: 0)
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
struct Item {
var _id = UUID()
var text = UUID().uuidString.prefix(5)
}