I have this SwiftUI code that builds a large UI view that's too big to fit on screen, so I've put it in a 2D scrollview. While this example is a grid, let's assume the content fills just as much but the items are more randomly placed.
When the view appears, I want it to start with having one of these items centered, in the case below the item with ID "(25,25)". This compiles fine, and looks fine by the docs, and runs, but does not scroll to the ZStack with ID "(25,25)".
Why does it not scroll to this ZStack?
The code was tested with both iOS 14 and iOS 15b1.
var body: some View {
ScrollViewReader { scrollView in
ScrollView([.horizontal, .vertical]) {
VStack(alignment: .leading, spacing: 0) {
ForEach(0..<50) { y in
HStack(alignment: .center, spacing: 0) {
ForEach(0..<50) { x in
ZStack {
Circle()
.frame(width: 35, height: 35)
.foregroundColor(.blue.opacity(0.5))
Text("(\(x),\(y)")
.font(.system(size: 8))
}
.id("(\(x),\(y))")
.offset(x: CGFloat(x * 25), y: CGFloat(y * 25))
}
}
}
}
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
scrollView.scrollTo("(25,25)")
}
}
}
}