GarySabo
GarySabo

Reputation: 6710

SwiftUI How to use .refreshable view modifier without a list?

iOS 15 introduces the '.refreshable' View Modifier, but most of the examples I've seen use a List. I want to implement a pull to refresh feature in an app that does not use a list but just a VStack with Text views like the below. How can I implement this so that pull down to refresh will refresh my data?

import SwiftUI

struct ContentView: View {

@State var songName = "Song"
@State var artistName = "Artist"

    var body: some View {
        VStack {
            Text(songName)
            Text(artistName)
        }
        .refreshable {
            reloadData()
        }
    }
    
    private func reloadData() {
        
        songName = "Let it Be"
        artistName = "The Beatles"
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 9

Views: 1362

Answers (0)

Related Questions