2

When ScrollView is initialized, the focus is on item number 6. When I do insert, the focus element is lower.

How can I make ScrollView not change the focus on the element? I want the focus to still remain on element 6 when inserting. That is, so that visually nothing changes on the screen.

@State var items = [6, 7, 8, 9, 10]
@State var newItems = [1, 2, 3, 4, 5]

Button {
    items.insert(contentsOf: newItems, at: 0)
} label: {
    Text("Insert")
}

ScrollView {
    ForEach(items, id: \.self) { item in
        Text("\(item)")
    }
}
Clockdev
  • 21
  • 2
  • You need `ScrollViewReader` and tracking visible top. Next should be helpful https://stackoverflow.com/a/65871577/12299030. – Asperi Jun 27 '22 at 15:41

0 Answers0