BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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?

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.