Skip to content

A flexible, customisable SwiftUI pull-to-refresh for iOS 14 and above.

Notifications You must be signed in to change notification settings

gormster/PullToRefresh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PullToRefresh

This package adds far more flexible pull-to-refresh functionality to SwiftUI, for iOS 14 and 15.

RefreshableScrollView

  • Usable in any view through a pair of view modifiers
  • Convenient RefreshableScrollView container
  • Allows full customisation of the refreshing view

Using RefreshableScrollView

Replace your ScrollView with RefreshableScrollView and add the refreshCallback: parameter.

class ViewModel {
    func startRefresh(completion: @escaping () -> Void) {
        // Do something...
        
        completion()
    }
}
RefreshableScrollView(refreshCallback: viewModel.startRefresh(completion:)) {
    YourScrollContentView()
}

Customising the refresh view

You can use any view for the refresh view; just throw in an environment variable to watch the pull to refresh state and you're done.

struct MyCustomRefreshView: View {
    @Environment(\.pullToRefreshState) var refreshState
    
    // Your custom view body goes here!
}

Then, pass it in as a parameter to RefreshableScrollView or pullToRefreshContainer().

RefreshableScrollView(refreshCallback: viewModel.startRefresh(completion:), refreshView: MyCustomRefreshView()) {
    YourScrollContentView()
}

Customising PullToRefreshView

You can easily change the indicator views for the default PullToRefreshView, too.

let refreshView = PullToRefreshView(indicatorView: Image(systemName: "arrow.clockwise.circle.fill"))

Using the modifiers

If you want to apply pull-to-refresh to a different type of view, you can use the view modifiers pullToRefreshContent() and pullToRefreshContainer. The former goes on the content (as in the thing that the user drags with their finger) and the latter goes on the container (as in the thing that stays still when the user drags the content). If your container takes multiple views, apply the content modifier to the one at the top.

MyCustomScrollView {
    Color.clear.pullToRefreshContent()
    
    ForEach(items) { item in
        YourListItemView(item)
    }
}
.pullToRefreshContainer { viewModel.startRefresh(completion: $0) }
.clipped()

About

A flexible, customisable SwiftUI pull-to-refresh for iOS 14 and above.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages