I am developing an application with swiftui. After zooming, when I say scroll to the corner with the ScrollViewReader, it goes out of the screen. my code is below. It fails after trying a few times. it doesn't do it every time.
import SwiftUI
struct ContentView: View {
@State var zoomIn = false
var body: some View {
GeometryReader { g in
ScrollViewReader { reader in
ScrollView([.horizontal,.vertical], showsIndicators: false) {
VStack(spacing: 20) {
ForEach(0 ..< 11, id:\.self) { row in
HStack(spacing: 20) {
ForEach(0 ..< 11, id:\.self) { column in
Text("Item \(row) \(column)")
.foregroundColor(.white)
.frame(width: zoomIn ? 70 : 35, height: zoomIn ? 70 : 35)
.background(Color.red)
.id("\(row)\(column)")
.onTapGesture {
withAnimation {
reader.scrollTo( ["00", "010","100","1010"].randomElement()!)
}
}
}
}
}
}
Button("Zoom") {
withAnimation {
zoomIn.toggle()
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
my home screen.
after scrollTo