Reputation: 197
In my SwiftUI app I've a List with nested ScrollView, since I've updated my iPhone to iOS 16 the refresh on the main List has a strange behavior. It seems that every ScrollView has their own refresh. The issue is that I've applied the .refreshable modifier on the main list, not on the nested ones.
Before iOS 16 the problem did not exist. So, is a bug or we can fix it is some way?
Here is the code and a short video:
List {
VStack(alignment: .leading) {
Text("Line 1")
.font(.title)
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 20) {
Text("Item 1")
Text("Item 2")
Text("Item 3")
Text("Item 4")
Text("Item 5")
Text("Item 6")
Text("Item 7")
Text("Item 8")
Text("Item 9")
Text("Item 10")
}
.frame(height: 180)
}
}
.listRowSeparator(.hidden)
VStack(alignment: .leading) {
Text("Line 2")
.font(.title)
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 20) {
Text("Item 1")
Text("Item 2")
Text("Item 3")
Text("Item 4")
Text("Item 5")
Text("Item 6")
Text("Item 7")
Text("Item 8")
Text("Item 9")
Text("Item 10")
}
.frame(height: 180)
}
}
.listRowSeparator(.hidden)
}
.listStyle(.plain)
.refreshable {
print("refresh!")
}
Upvotes: 8
Views: 1169
Reputation: 923
I'm assuming this is the answer you are looking for: https://stackoverflow.com/a/75834493
So on your nested List or ScrollView you'd add this modifier:
List/ScrollView {
}.environment(\EnvironmentValues.refresh as! WritableKeyPath<EnvironmentValues, RefreshAction?>, nil)
Upvotes: 1
Reputation: 11
If you don't want to switch to a ScrollView from a List (because ScrollView doesn't reuse cells), using .introspectScrollView
helped me. For a scrollView inside a list set isDirectionalLockEnabled
to true and bounces
as false to avoid this issue.
.introspectScrollView { scrollView in
scrollView.isDirectionalLockEnabled = true
scrollView.bounces = false
}
Upvotes: 1
Reputation: 11
try this on your nested ScrollViews ;)
.environment(\EnvironmentValues.refresh as! WritableKeyPath<EnvironmentValues, RefreshAction?>, nil)
Upvotes: 0
Reputation: 3498
If you change your List
for a ScrollView
, and then change the line .listStyle(.plain)
for .padding(.horizontal)
, it will work the same with no bug.
Upvotes: 4