How to programmatically scroll list SwiftUI ?

iOS14 * if we want to scroll the List programmatically in iOS We can use scrollTo(.. function where we can pass the specific index for jump to the List. for example if we need to scroll to last of list we can use customArray.endIndex - 1
likewise you can pass your desired index for example 4,5 etc. Below code explains for scrolling the list to bottom, try it yourself.

struct ContentView: View {
    var countries: [String] = ["India", "USA", "JAPAN","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia", "Herzegovina"]
    var body: some View {
        ScrollViewReader { scrollView in
                ForEach(countries, id: \.self) { countryName in
                        .padding(.all, 50)
                .onAppear {
                    scrollView.scrollTo(countries[countries.endIndex - 1])

Leave a Comment

Your email address will not be published. Required fields are marked *