Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
   }
}
like image 597
Geheiligt Avatar asked Nov 22 '25 15:11

Geheiligt


1 Answers

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()
              }
       }
}
like image 92
Ashley Mills Avatar answered Nov 25 '25 03:11

Ashley Mills