1

In the following code snippet, I use proxy.scrollTo() to scroll to a target. In order to animate the scrolling process, I wrapped this function call inside withAnimation. This code works on iOS 16, but not on iOS 17. In iOS 17, it does scroll, but without any animation. Is this a bug or is there an API change? Thanks!

import SwiftUI

struct ScrollTest: View {
    var body: some View {
        ScrollViewReader { proxy in
            List {
                Button("Scroll") {
                    withAnimation {
                        proxy.scrollTo(15, anchor: .top)
                    }
                }
                
                ForEach(1..<50) { i in
                    Text("Item \(i)")
                        .id(i)
                }
            }
        }
    }
}
wheatset
  • 339
  • 1
  • 7

3 Answers3

1

The issue just got fixed in iOS 17.0 build 21A5319a (Developer Beta 7). Animation is back for the scrollTo action.

liudasbar
  • 173
  • 4
  • 11
0

Okay so turns out there's been a deprecation in the API, ScrollViewReader is now deprecated and replaced with a new .scrollPosition() modifier.

struct ContentView: View {
    @State private var position: Int?
    
    var body: some View {
        ScrollView {
            LazyVStack {
                Button("Jump...") {
                    withAnimation {
                    position = 99
                 }
               }
                
                ForEach(0..<100) { index in
                    Rectangle()
                        .fill(Color.green.gradient)
                        .frame(height: 300)
                        .id(index)
                }
            }
            .scrollTargetLayout()
        }
        .scrollPosition(id: $position)
    }
}
devdchaudhary
  • 467
  • 2
  • 13
0

This was a bug that has been addressed in iOS 17 beta 5. Your code now scrolls with animation again.

Jordan H
  • 52,571
  • 37
  • 201
  • 351