SwiftUI List scroll position

Hello. Is it possible to get the current List scroll position in SwiftUI? I can see how to do this with ScrollView + VStack + GeometryReader * but this approach doesn't work correctly with Lists. Do I need to drill into the underlying UITableView / NSTableView / UIScrollView / NSScrollView? My primary target is iOS14+.

* With Scroll View this works perfectly, but I need List
import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                ForEach(0 ..< 200) { index in
                    Text("Hello World \(index)").font(.title2)
                }
                .background(
                    GeometryReader { geometry in
                        Color.pink.opacity(0.2).border(Color.blue, width: 2).cornerRadius(10)
                            .execute {
                                let offset = geometry.frame(in: .named("scroll")).minY
                                print(Int(offset))
                            }
                    }
                )
            }
            .coordinateSpace(name: "scroll")
        }
    }
}

extension View {
    func execute(callback: @escaping () -> Void) -> some View {
        callback()
        return self
    }
}

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Terms of Service

Privacy Policy

Cookie Policy