My problem is that i have (in SwiftUI) a ScrollView with an foreach inside. Know when the foreach loads all of my entries i want that the last entry is focused.
I did some google research, but i didn't find any answer.
ScrollView {
VStack {
ForEach (0..<self.entries.count) { index in
Group {
Text(self.entries[index].getName())
}
}
}
}
Solution for iOS14 with
ScrollViewReader
in iOS14 SwiftUI gains a new ability. To be able to scroll in a ScrollView up to a particular location. There is a new type called
ScrollViewReader
which works just likeGeometry Reader
.The code below will scroll to the last item in your View. I reused your code adding some color for better visualisation. So this is your struct for 'Entry' I guess:
And the main ContentView:
Try to run this in the new version of SwiftUI announced at WWDC20. I think it is a great enhancement.