Issue #761
Explicitly specify id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| ScrollView { ScrollViewReader { proxy in LazyVStack(spacing: 10) { ForEach(items) { item in Cell(item: item) .id(item.id) } } .padding() .onReceiveKeyboard(onNext: { onNext() if let item = selectedItem { proxy.scrollTo(item.id, anchor: .center) } }, onPrevious: { onPrevious() if let item = selectedItem { proxy.scrollTo(item.id, anchor: .center) } }) } }
|
I usually extract ScrollViewReader
into a helper function that use onChange
to react to state changes
1 2 3 4 5 6 7 8 9 10 11
| func scrollViewReader<Content: View>(@ViewBuilder content: @escaping () -> Content) -> some View { ScrollViewReader { proxy in content() .onChange(of: itemsHolder.focusItem) { item in guard let item = item else { return } withAnimation { proxy.scrollTo(item.id, anchor: .center) } } } }
|
Updated at 2021-02-21 19:14:01