skip to Main Content

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!")
}

Video of the issue

3

Answers


  1. 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.

    Login or Signup to reply.
  2. try this on your nested ScrollViews 😉

    .environment(EnvironmentValues.refresh as! WritableKeyPath<EnvironmentValues, RefreshAction?>, nil)
    
    Login or Signup to reply.
  3. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search