I try to set a ScrollView to the top most element with a button. Therefor the new .scrollPosition() modifier should come in handy. I can't get it to work tho. In the following example the scrolling works when clicking on an item or the button at the bottom of the stack. But when scrolling the value is not updated. You can reproduce the behaviour by scrolling to the bottom, click the button "Scroll to top" and scroll back down. The second time, nothing happens because the value is still on 1. Am I doing something wrong or is my expectation of the modifier wrong? The documentation says so: https://developer.apple.com/documentation/swiftui/view/scrollposition(id:anchor:)
//
// ContentView.swift
// Test
//
// Created by Sebastian Götte on 23.09.23.
//
import SwiftUI
struct ContentView: View {
@State private var scrollViewPosition: Int?
var body: some View {
ScrollView(.vertical) {
VStack(spacing: 32) {
ForEach(1..<21) { item in
Text("I am item no. \(item)")
.id(item)
.frame(maxWidth: .infinity)
.padding()
.background(.purple)
.cornerRadius(4)
.onTapGesture {
withAnimation {
scrollViewPosition = item
}
}
}
Button("Scroll to top", action: {
withAnimation {
scrollViewPosition = 1
}
})
}
.scrollTargetLayout()
.scrollTargetBehavior(.viewAligned)
.padding()
}
.scrollPosition(id: $scrollViewPosition, anchor: .top)
.onChange(of: scrollViewPosition) { oldValue, newValue in
print(newValue ?? "No value set")
}
}
}
VStackin theScrollView. You need to use aLazyVStack.