1

Problem

  • First time when the button "Go to 990" is tapped the scroll view doesn't scroll to 990 on top. (see screenshot below)
  • The anchor is not respected.
  • Second time it scrolls to 990 top correctly.

Questions

  1. What is wrong and how can it be fixed?

Versions

  • Xcode 14

Code

struct ContentView: View {
    
    @State private var scrollProxy: ScrollViewProxy?
    
    var body: some View {
        NavigationStack {
            ScrollViewReader { proxy in
                List(0..<1000, id: \.self) { index in
                    VStack(alignment: .leading) {
                        Text("cell \(index)")
                            .font(.title)
                        Text("text 1")
                            .font(.subheadline)
                        Text("text 2")
                            .font(.subheadline)
                    }
                }
                .onAppear {
                    scrollProxy = proxy
                }
            }
            .toolbar {
                ToolbarItem {
                    Button("Go to 990") {
                        scrollProxy?.scrollTo(990, anchor: .top)
                    }
                }
            }
        }
        #if os(macOS)
        .frame(minWidth: 500, minHeight: 300)
        #endif
    }
}

Screenshot

After tapping on Go to 990, doesn't scroll to 990 top

user1046037
  • 16,755
  • 12
  • 92
  • 138
  • 1
    you could try adding `.id(index)` to the `VStack`. – workingdog support Ukraine Sep 22 '22 at 05:54
  • Thanks, I tried embedding the list inside the VStack and added id to the cell, but doesn't work. Could you try it – user1046037 Sep 22 '22 at 05:57
  • your code seems to work well for me, even without the `.id(index)`, on macos 13 Ventura, using xcode 14.1-beta, target macCatalyst and ios-16. Tested on real devices, not Previews. It may be different on older systems. My previous comment is to add `.id(index)` to the VStack you have. – workingdog support Ukraine Sep 22 '22 at 06:00
  • I am using Xcode 14.1 beta 2 on macOS 13.0 Beta (22A5352e) and doesn't work. Which Xcode beta (1 or 2) and which macOS 13 Beta are you using? I also tried adding `index` to the VStack – user1046037 Sep 22 '22 at 06:10
  • @workingdogsupportUkraine Please test using iPhone 14 Pro simulator, also you could try running the app on macOS 13 Ventura. – user1046037 Sep 22 '22 at 06:15
  • @workingdogsupportUkraine The anchor (top) is not respected, it just scrolls, but doesn't scroll to top though it can. Second time it works as expected – user1046037 Sep 22 '22 at 06:18
  • 1
    I'm on macos 13.0 Beta (22A5352e), using xcode 14.1-beta2. Yeah, on the simulator 14 pro, the first time, it scrolls to 990 in the middle of the screen. The second time, it is at the top. – workingdog support Ukraine Sep 22 '22 at 06:27
  • Exactly, that is the same issue I am facing as well. I would expect it to scroll to top the first time (because that is what is mentioned in the code and it is possible). – user1046037 Sep 22 '22 at 06:32
  • Same behaviour on mac Catalyst, but on macos only, it scrolls, but well short of 990 the first time, but to 990 on top the second time. Could be a bug. – workingdog support Ukraine Sep 22 '22 at 06:33
  • Is this a bug? Is there a workaround to fix this? – user1046037 Sep 22 '22 at 06:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248243/discussion-between-user1046037-and-workingdog-support-ukraine). – user1046037 Sep 22 '22 at 06:34
  • seeing similar behaviour on ios16...scrollView is not moving to top – aaronium112 Jan 09 '23 at 17:58
  • I have filed a feedback, this seems like a bug with the framework. Hopefully it gets fixed – user1046037 Jan 10 '23 at 00:25

0 Answers0