SwiftUI Introspect library pull to refresh issue

1.1k Views Asked by At

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?()
    }
1

There are 1 best solutions below

3
Mariam Sargsyan On

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