The article on SwiftUI Pull to Refresh in the Navigation Bar suggests using a UIKit solution, specifically the UIRefreshControl, to implement Pull to Refresh functionality in SwiftUI.

By utilizing an extension provided in the article, you can adapt the iOS 15 refreshable modifier for iOS 13 and iOS 14, allowing you to use the same code across different iOS versions.

Alternatively, the article also presents a closure-based API that avoids the use of the async-await API, providing an alternative approach to implement Pull to Refresh in SwiftUI.

Although the refreshable modifier in iOS 15 simplifies implementing Pull to Refresh in SwiftUI, it’s important to note that it is not available in earlier versions of SwiftUI.

Interestingly, Apple made Swift Concurrency available in iOS 13 (as mentioned in the Xcode 13.2 Release Notes).

This article addresses the needs of codebases that are limited to using SwiftUI 1 (iOS 13) or SwiftUI 2 (iOS 14) and are searching for a solution to support Pull to Refresh functionality uniformly across different SwiftUI versions.

By providing an alternative approach, this article proves valuable for developers who want to implement Pull to Refresh in a consistent manner throughout their codebase.

struct ContentView: View {
    
	...
    
    var body: some View {
        List {
            ...
        }
        .refreshable {
            await viewModel.fetch()
        }
    }
}

Alternatively, you can opt into the closure-based API below to spare using async await API.

struct ContentView: View {
    
	...
    
    var body: some View {
        List {
            ...
        }
        .onRefresh { refreshControl in
            viewModel.fetch {
                refreshControl.endRefreshing()
            }
        }
    }
}

Quick Start

See details in OnRefreshModifier.swift and RefreshableModifier.swift in Refreshable package. Find the examples above in the Examples folder.

For your own projects, simply use Refreshable Swift Package.

Download

SwiftUI Pull to Refresh

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *