While trying the new ScrollPosition
API I noticed that scrollTo(id: anchor:)
behaves different than ScrollViewProxy.scrollTo(_: anchor:)
.
Consider the following example:
struct ContentView: View {
@State private var position = ScrollPosition(edge: .top)
var body: some View {
NavigationStack {
ScrollViewReader { proxy in
ScrollView {
VStack(spacing: 8) {
ForEach(1..<100) { index in
Text(verbatim: index.formatted())
.frame(maxWidth: .infinity)
.background(.gray)
.id(index)
}
}
}
.scrollPosition($position)
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Spacer()
Button("50 (T)") {
withAnimation {
position.scrollTo(id: 50, anchor: .top)
// proxy.scrollTo(50, anchor: .top)
}
}
Button("50 (B)") {
withAnimation {
position.scrollTo(id: 50, anchor: .bottom)
// proxy.scrollTo(50, anchor: .bottom)
}
}
Spacer()
}
}
}
}
}
}
The position
methods don't align top and bottom edges, but the proxy
ones do.
Is this expected or is it a bug?