UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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())
 ...

4      

@7784  

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

2      

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)
                            }
                    } 

2      

@GerryCSwift - I got same issue. Did you find a solution?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.