SwiftUI pull to refresh: Easy guide

SwiftUI Pull-to-Refresh is an essential feature in most apps, people just expect to be able to pull to refresh on lists of data. Pull to refresh allows users to update content effortlessly by pulling down on a view. 

In this blog post, we will explore the implementation of SwiftUI Pull to Refresh, its benefits, and the steps to integrate this feature into your application.

What is the pull to refresh modifier in SwiftUI?

Pull-to-Refresh is a user interface pattern that enables users to refresh content within an app by pulling down on the screen. In SwiftUI, we can easily add this feature to our application by using the refreshable modifier.

When you first initiate a List you fill it with data, properly from some API, and inside your pul to refresh method you execute the same API and repopulate the data.

How to add pull to refresh in SwiftUI?

To add pull to refresh feature to our application we first need to wrap our data inside a List and then use the .refreshable() modifier.

In the following example, we will create a list with the refreshable modifier that adds one more item to the list:

import SwiftUI

struct PullToRefreshView: View {
    @State var listItems: [String] = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
    
    var body: some View {
        List(listItems, id: \.self) { item in
            Text(item)
        }
        .refreshable {
            listItems.append("Item 6")
        }
    }
}

The result:

Wrap up Pull to Refresh

As you can see it’s pretty straightforward to implement pull to refresh in a SwiftUI application. And thank you Apple for making it so easy for us because pull to refresh is a natural and intuitive way to update content within an app.

I hope you can use this short tutorial in your next application 🙂

Scroll to Top