SwiftUI list does not scroll using 2-finger swipe on trackpad

225 Views Asked by At

I have been testing this simple SwiftUI code: It has a list in ContentView. When a cell of the list is tapped, it pushes a SecondList into the navigation stack. When this SwiftUI code was run on a real iPad with a trackpad, I used 2-finger swipe to scroll the list up and down. The list in ContentView scrolls smoothly. However, for SecondList, it may freeze randomly and thereafter not responding.

It also failed in a UIKit project, where I replaced a UINavigationView with this ContentView using UIHostingViewController.

It was testing it with iPadOS 14.5 on both Simulator and iPad Pro hardware with Magic Keyboard.

How do I fix this? Thank you.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<30, id: \.self) { index in
                    NavigationLink(
                        destination: SecondList(),
                        label: {
                            Text(String(index))
                        })
                }
            }
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SecondList: View {
    var body: some View {
        List {
            ForEach(0..<30, id: \.self) { index in
                Text(String(index))
            }
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
1

There are 1 best solutions below

1
On

Use a scrollView

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<30, id: \.self) { index in
                    NavigationLink(
                        destination: SecondList(),
                        label: {
                            Text(String(index))
                        })
                }
            }
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SecondList: View {
    var body: some View {
        scrollView {
            List {
                ForEach(0..<30, id: \.self) { index in
                    Text(String(index))
                }
            }
        }
    }
}