Skip to content

Pull to refresh functionality for any ScrollView in SwiftUI!

License

Notifications You must be signed in to change notification settings

szluyufeng/swiftui-pull-to-refresh

 
 

Repository files navigation

SwiftUIPullToRefresh

Pull to refresh is a common UI pattern, supported in UIKit via UIRefreshControl. (Un)surprisingly, it's also unavailable in SwiftUI.

This package contains a component - RefreshableScrollView - that enables this functionality with any ScrollView. It also doesn't rely on UIViewRepresentable. The end result will look like this:

in action

Recipe

Check out this recipe for in-depth description of the component and its code. Check out SwiftUIRecipes.com for more SwiftUI recipes!

Sample usage

struct TestView: View {
  @State private var now = Date()

  var body: some View {
     RefreshableScrollView(onRefresh: { done in
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
          self.now = Date()
          done()
        }
      }) {
        VStack {
          ForEach(1..<20) {
            Text("\(Calendar.current.date(byAdding: .hour, value: $0, to: now)!)")
               .padding(.bottom, 10)
           }
         }.padding()
       }
     }
   }
}

Installation

This component is distrubuted as a Swift package.

About

Pull to refresh functionality for any ScrollView in SwiftUI!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%