Pull-to-Refresh in iOS 18 – #30DaysOfSwift

Day 20: Adding a Pull-to-Refresh Feature to Your Lists or Views 🔄

Let’s implement the pull-to-refresh feature in SwiftUI, a common gesture that enhances user experience by allowing users to refresh content easily.

Implementing Pull-to-Refres…


This content originally appeared on DEV Community and was authored by Vaibhav Dwivedi

Day 20: Adding a Pull-to-Refresh Feature to Your Lists or Views 🔄

Let's implement the pull-to-refresh feature in SwiftUI, a common gesture that enhances user experience by allowing users to refresh content easily.

Implementing Pull-to-Refresh

To add a pull-to-refresh feature, we can use the .refreshable modifier available in SwiftUI. This modifier simplifies the implementation process and allows us to handle the refresh action with minimal code.

Image description

Code Example

import SwiftUI

struct ContentView: View {
    @State private var items = ["Item 1", "Item 2", "Item 3"] // Sample data
    @State private var isLoading = false // State to manage loading state

    var body: some View {
        NavigationView {
            List(items, id: \.self) { item in
                Text(item) // Display items in the list
            }
            .navigationTitle("Pull to Refresh")
            .refreshable {
                await loadData() // Call the async function to load data
            }
        }
    }

    func loadData() async {
        isLoading = true // Set loading state
        // Simulate network delay
        try? await Task.sleep(nanoseconds: 2 * 1_000_000_000) // Wait for 2 seconds
        // Update items with new data
        items.append("Item \(items.count + 1)") // Add a new item for demonstration
        isLoading = false // Reset loading state
    }
}

@main
struct PullToRefreshApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() // Main content view
        }
    }
}

The full series is available on my profile and the components can also be found at shipios.app/components.

Happy Coding! 🎊


This content originally appeared on DEV Community and was authored by Vaibhav Dwivedi


Print Share Comment Cite Upload Translate Updates
APA

Vaibhav Dwivedi | Sciencx (2024-10-29T16:36:00+00:00) Pull-to-Refresh in iOS 18 – #30DaysOfSwift. Retrieved from https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/

MLA
" » Pull-to-Refresh in iOS 18 – #30DaysOfSwift." Vaibhav Dwivedi | Sciencx - Tuesday October 29, 2024, https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/
HARVARD
Vaibhav Dwivedi | Sciencx Tuesday October 29, 2024 » Pull-to-Refresh in iOS 18 – #30DaysOfSwift., viewed ,<https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/>
VANCOUVER
Vaibhav Dwivedi | Sciencx - » Pull-to-Refresh in iOS 18 – #30DaysOfSwift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/
CHICAGO
" » Pull-to-Refresh in iOS 18 – #30DaysOfSwift." Vaibhav Dwivedi | Sciencx - Accessed . https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/
IEEE
" » Pull-to-Refresh in iOS 18 – #30DaysOfSwift." Vaibhav Dwivedi | Sciencx [Online]. Available: https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/. [Accessed: ]
rf:citation
» Pull-to-Refresh in iOS 18 – #30DaysOfSwift | Vaibhav Dwivedi | Sciencx | https://www.scien.cx/2024/10/29/pull-to-refresh-in-ios-18-30daysofswift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.