1

I want to implement pull to refresh in my scrollview in SwiftUi. So I have been trying Introspect library to use its .introspectScrollView and use refresh controller. But I have been getting an issue with it, the refresh progress spinner immediately gets hidden in my end. So I wanted to know how to keep showing the refresh spinner until the data loads.

Also please note I am not using list as I have subViews inside the ScrollView, so I am still using ScrollView.

My code is -

   ScrollView{
     //subView1
     //subView2
   }.introspectScrollView { scrollView in
           let control = UIRefreshControl()
           control.addTarget(viewModel, action: #selector(viewModel.loadData1), for: .valueChanged)
           scrollView.refreshControl = control
           viewModel.isRefreshed = {
                control.endRefreshing()
           }
     }


   //In view model 
   var isRefreshed: (() -> ())?
   func loadData1(){
      // load data 1
      loadData2()
    }
   func loadData2(){
      //load data2
       
      isRefreshed?()
    }
21
  • Thanks for clarifying. And the negative vote is not mine. Like I suggested in the other question, if you use List instead of UIScrollView, you can use the refreshable modifier. Commented Mar 2, 2022 at 16:23
  • Let me check if I can use List instead of UIScrollView as there is difference in visual in both the two Commented Mar 2, 2022 at 16:26
  • 1
    developer.apple.com/documentation/SwiftUI/View/… Commented Mar 3, 2022 at 0:33
  • @loremipsum This is for list in SwiftUi but not for ScrollView. Commented Mar 5, 2022 at 18:51
  • 1
    How about putting a scrollview as the only subview of List? Then you can make all your fancy subviews inside the scrollview, and have the pull to refresh functionality with the List. Commented Mar 7, 2022 at 17:19

1 Answer 1

3

You could try the SwiftUIRefresh package, that includes both Introspect and SwiftuiRefresh packages. So, add https://github.com/timbersoftware/SwiftUIRefresh.git link to your package and use this like

import SwiftUI
import SwiftUIRefresh

struct ContentView: View {
    
    @State private var isShowing = false
    var body: some View {
        List {
            Text("Item 1")
            Text("Item 2")
        }
        .pullToRefresh(isShowing: $isShowing) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                self.isShowing = false
            }
        }
    }
}

Source code from: https://github.com/siteline/SwiftUIRefresh

Sign up to request clarification or add additional context in comments.

3 Comments

Yes but I don't want to use this library as of now, I am using a different library now
@Manas you don’t have to use that library, you can look at how it works and use that knowledge to fix your problem.
This is for list not for scrollview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.