Search code examples
swiftswiftuiscrollviewgesture

Adding action for ScrollView reached end and continue pulling SwiftUI


I have horizontal ScrollView and ForEach in it to display some data from array. Is there any opportunity to make something like "Pull to refresh" but from the other side of scroll view (bottom instead top or in my case from the left)? In other words, to add action to gesture when user already reached end of the ScrollView but continue pulling.

ScrollView(.horizontal, showsIndicators: false) {
   HStack(spacing: 15) {
                ForEach(viewModel.recomendations, id:  \.placeID) { 
    rec in ...
   }
}

Solution

  • The easiest way I've found to do this is to add an extra view to the end of the scroll view, which is only displayed if more content is available. Then use the .task modifier to load the next page of data, e.g.

    ScrollView(.horizontal, showsIndicators: false) {
       HStack(spacing: 15) {
           ForEach(viewModel.recomendations, id:  \.placeID) { 
              rec in ...
           }
           if viewModel.moreRecordsAvailable {
               ProgressView()
                  .task {
                     await viewModel.loadNextPage()
                  }
           }
    }