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?()
}
Listinstead ofUIScrollView, you can use therefreshablemodifier.