i have used simple listing of data using List
. I would like to add pull down to refresh functionality but i am not sure which is the best possible approach.
Pull down to refresh view will only be visible when user tries to pull down from the very first index same like we did in UITableView
with UIRefreshControl
in UIKit
Here is simple code for listing data in SwiftUI
.
struct CategoryHome: View {
var categories: [String: [Landmark]] {
.init(
grouping: landmarkData,
by: { $0.category.rawValue }
)
}
var body: some View {
NavigationView {
List {
ForEach(categories.keys.sorted().identified(by: \.self)) { key in
Text(key)
}
}
.navigationBarTitle(Text("Featured"))
}
}
}
The swiftui-introspects has not supported on masOS yet, so if you are going to build a UI that works for both iOS and macOS, consider the Samu Andras library.
I forked his code, added a few enhancements, and added the ability to use without the NavigationView
Here is the sample code.
For more details, you can visit the link below. https://github.com/phuhuynh2411/SwiftUI-PullToRefresh
Hi check out this library I made: https://github.com/AppPear/SwiftUI-PullToRefresh
You can implement it by one line of code:
I needed the same thing for an app I'm playing around with, and it looks like the SwiftUI API does not include a refresh control capability for
ScrollView
s at this time.Over time, the API will develop and rectify these sorts of situations, but the general fallback for missing functionality in SwiftUI will always be implementing a struct that implements
UIViewRepresentable
. Here's a quick and dirty one forUIScrollView
with a refresh control.But of course, you can't use any SwiftUI components in your scroll view without wrapping them in a
UIHostingController
and dropping them inmakeUIView
, rather than putting them in aLegacyScrollView() { // views here }
.Here's an implementation that introspects the view hierarchy and adds a proper
UIRefreshControl
to a SwiftUI List's table view: https://github.com/timbersoftware/SwiftUIRefreshBulk of the introspection logic can be found here: https://github.com/timbersoftware/SwiftUIRefresh/blob/15d9deed3fec66e2c0f6fd1fd4fe966142a891db/Sources/PullToRefresh.swift#L39-L73