Programmatically show the pull to refresh activity indicator of a refreshable List
Asked Answered
B

0

6

I wonder if there's a simple way to make visible the activity indicator used by the refresh control of a SwiftUI List? This would provide a consistent appearance of a refreshing UI, regardless of how the refresh was initiated.

After enabling pull to refresh on a List by using the refreshable modifier, the Lists environment has its refresh item set, which is a RefreshAction used by the refresh control. My guess was that using this RefreshAction would maybe also trigger the visibility of the refresh control, which I tried:

struct MyView : View {
    @State var content: Content
    
    var body: some View {
        _MyView(content: $content)
            .refreshable { // assigning `refresh` to the environment
                await refresh()
            }
    }
    
    private func refresh() async {
        // ...
        content = newContent
        // ...
    }
}

struct _MyView : View {
    @Environment(\.refresh) private var refresh
    // [ content binding, etc... ]

    var body: some View {
        List {
            // ...
        }
        .onAppear {
            Task { await refresh?() } // doesn't show the activity indicator of the list
        }
    }
}

This didn't work.

Since I can't find a isRefreshing/refreshState or similar property of EnvironmentValues I'm wondering if maybe there's another (simple) way to trigger its appearance?

Brockway answered 21/8, 2022 at 19:52 Comment(1)
There is no build in possibility to achieve this. Here are some answers how you could implement your own solution.Hobby

© 2022 - 2024 — McMap. All rights reserved.