Home > Enterprise >  SwiftUI : How I can set refreshable for my Scrollview without List
SwiftUI : How I can set refreshable for my Scrollview without List

Time:03-14

import SwiftUI

struct HomeView: View {
    
    @StateObject var vm = NewsViewModel()
    
    let dataService = NewsDataService.instance
    
    init() {
        dataService.apiCall(text: "Korea")
    }
    
    var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false) {
                SearchView()
                VStack {
                    Divider()
                        if let newsArray = vm.newsArray?.articles {
                            ForEach(newsArray) { news in
                                NewsRowView(news: news)
                            }
                        }
                }
            }
            .navigationTitle("News")
            .navigationBarTitleDisplayMode(.automatic)
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

Hi, I tried to find refresh for my scrollview. But, most of the examples were capable with List.

As you can see my code, I makde my list with HStack children View. Because of that, I can't use the ".refreshable" modifier.

So, I want to know how I can set that in my view.

And If I can, I want to see detailed code as well!

Thanks.

CodePudding user response:

You could use refreshable like this in your NewsRowView. Keep in mind you would need to make sure you are using ObservableObject where necessary so that it will give you the new data once it's updated.

.refreshable {
            Task {
                let dataService = NewsDataService.instance
                dataService.apiCall(text: "Korea")                    
            }
}
  • Related