I have set a .refreshable modifier on one of my views (attached to a VStack). This view has NavigationLinks to other pages, but unexpectedly the refreshable still works in the next pages on the NavigationStack.
i.e. I can still pull to refresh in any page that was linked from the first page where I declared the .refreshable…
I would like this .refreshable to apply only to the first page, and not to the rest of the pages.
How do I do that, and why does it behave like that in the first place?
The code on the first View/page looks like this:
VStack {
NavigationStack {
List {
//Some code with list elements
}
}
}
.refreshable {
await vm.loadUserStories(getUrl: url)
3
Answers
From the
.refreshable
documentation:So the modifier puts your closure into the SwiftUI environment. That’s why all your pages use it.
Reading on:
So the answer is, put the modifier directly on the
List
instead of on theNavigationStack
. Then only the environment of theList
(and its subviews) will contain theRefreshAction
, and it won’t be visible to your other pages.It’s better to put the NavigationStack to be the first container not the VStack like this
this should work, you can use NavigationView if you you want to support iOS versions less than iOS 16 but it’s deprecated in iOS 16+
16.4 will fix this bug
iOS & iPadOS 16.4 Beta Release Notes