ScrollTo broken when scrolling to items within nested ForEach

Originator:expeditionpie
Number:rdar://FB9932681 Date Originated:2/27/2022
Status:Open Resolved:
Product:SwiftUI Product Version:
Classification: Reproducible:Yes
 
ScrollViewProxy's scrollTo function fails to correctly scroll to the desired view when that view is embedded in a ForEach that is itself embedded in a ForEach.
ie. ScrollView - ForEach - ForEach - View.

example:

import SwiftUI
import PlaygroundSupport
let outerCount = 10
let innerCount = 2

struct MainView: View {
    var body: some View {
        ScrollViewReader { proxy in
            ScrollView {
                ForEach(0..<outerCount, id:\.self) { num in
                    VStack {
                        Text("\(num)").padding()
                        InnerView(start: num, proxy: proxy)
                    }
                    .frame(width:300)
                    .background(.red)
                    .onTapGesture {
                        print(num)
                        withAnimation {
                            proxy.scrollTo(num, anchor: .top)
                        }
                    }
                    .padding()
                }
            }.frame(height:800)
        }
    }
}

struct InnerView: View {
    var start: Int
    var proxy: ScrollViewProxy
    var body: some View {
        VStack {
            ForEach(start*outerCount+outerCount..<start*outerCount+innerCount+outerCount, id:\.self) { num in
                Text("\(num)")
                    .frame(maxWidth:.infinity)
                    .frame(height: 50)
                    .background(.blue)
                    .onTapGesture {
                        print(num)
                        withAnimation {
                            proxy.scrollTo(num, anchor: .top)
                        }
                    }
                    .padding(3)
            }
        }
    }
}

let view = MainView()
PlaygroundPage.current.setLiveView(view)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!