NEW: Learn SwiftData for free with my all-new book! >>

ScrollViewProxy.scrollTo seems to be broken on iOS 16

Forums > SwiftUI

Hi!

This simple app should scroll to the last line of a list after appending a new item. The code works fine with iOS 15 but cause a runtime error on iOS 16.

import SwiftUI

struct ScrollListView: View {
    @State private var data: [String] = []

    var body: some View {
        VStack {
            Button(action: {
                data.append("ScrollItem #\(data.count+1)")
            }) {
                Label("Add Row", systemImage: "plus")
            }
            .buttonStyle(.borderedProminent)

            ScrollViewReader { proxy in
                List(0..<data.count, id: \.self) { index in
                    Text(data[index]).id(index)
                }
                .listStyle(.grouped)
                .navigationTitle("ScrollList Test")
                .onChange(of: data.count) { _ in
                    proxy.scrollTo(data.count-1)
                }
            }
        }
        .onAppear() {
            for _ in 0..<30 {
                data.append("ScrollItem #\(data.count+1)")
            }
        }
    }
}

The error occures on line: proxy.scrollTo(data.count-1):

0x0000000100fb8050 in closure #2 in closure #3 in closure #1 in ScrollListView.body.getter at /…/TestApp/Features/Scrollview/ScrollTestView.swift:29

Does anyone have an idea or a workaround for iOS 16?

   

A workaround for this bug is to add a UUID id to the List. It causes it to be recreated every time data updates, but the scrolling works properly.

  List(0..<data.count, id: \.self) { index in
      Text(data[index]).id(index)
  }
  .id(UUID())
 ...

2      

@7784  

Great workaround, I don't suppose there's anyway to keep the animation? Mine just jumps to the top.

   

I have been struggling with this. Thanks.

However I think I am missing something. I have a List - ForEach and don't seem to make the scrollTo work (but no crashes at least. Any ideas on where I am going wrong in this code?

ScrollViewReader { proxy in
                        List {
                            ForEach(scoredSkippers, id: \.self) { skip in
                                HStack {
                                    VStack {
                                        Image(systemName: "\(Int(skip.raceRawScore)).circle.fill")
                                        Text(skip.raceLetterScore == .👍 ? "" : " \(skip.raceLetterScore.rawValue)").font(.caption2)
                                    }
                                    Spacer()
                                    Text(String(skip.sailNum))
                                    Spacer()
                                }
                                .onLongPressGesture {
                                    isShowingRaceScore = skip
                                }
                                .listRowBackground(skip.raceLetterScore != .👍 ? Color(.systemYellow) : nil)

                            }
                            .onMove(perform: move)
                        }
                        .id(UUID())
                        .scrollContentBackground(.hidden)
                        .background(.green.opacity(0.05))
                        .sheet(item: $isShowingRaceScore) { skip in
                            UpdateScoreView(thisRscore: skip)
                            }
                        .environment(\.editMode, .constant(.active))
                        .onChange(of: scoredSkippers.count) { _ in
                            proxy.scrollTo(scoredSkippers.count - 1)
                            }
                    } 

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.