Scroll to Bottom of List in iOS 16 Beta / Xcode 14 Beta 3

I'm trying to add an item to the bottom of a List and then scroll that added item into view. I've made the following code that can be entered directly into a new iOS project in Xcode (14 beta 3) to replicate the problem (which is an exception after adding an item 3 times). I have tried many variations of the ID used in the "id" field of ForEach, the ".id" modifier on the text, as well as in the "scrollTo" method. Nothing works for me -- I get the same behavior no matter what I use: The first item scrolls into view as desired; the second item does not scroll; and the third item crashes.

I've left in the code a ".onDelete" modifier because it literally takes 3 items added before the crash will occur. So, you could add 2 items, delete one of those, and then it would take two more adds (still for a total of 3 items in the List) to see the crash. I'm baffled. It doesn't seem to matter how many items I start the list with (4 in this case)... it is always the third addition that crashes.

Any thoughts? Thanks for any help.

struct ContentView: View {
    
    @State var data = [1, 2, 3, 4]
    @State var count = 4

    var body: some View {

        VStack {
            ScrollViewReader { proxy in
                List {
                    ForEach(data, id: \.self) { datum in
                        Text("\(datum)")
                            .id(datum)
                    }
                    .onDelete {
                        data.remove(atOffsets: $0)
                    }

                }
                .onChange(of: data) { _ in
                    proxy.scrollTo(data.last)
                }
            }

            Button {
                count += 1
                data.append(count)
            } label: {
                Text("Add")
            }
        }
    }
}
Post not yet marked as solved Up vote post of Benjy1000 Down vote post of Benjy1000
2.8k views
  • I'm still seeing this in iOS 16.1 (FB11826112)

Add a Comment

Replies

I have a bug with uitextview. Seems that uiscrollview has some defect in ios 16.

Did you found a solution? I have the same problem here.

  • As of iOS 16.1 beta 2, this is still broken for me. To work around it, I just took the scrolling out, which is not ideal.

Add a Comment

I am using ScrollView instead of List inside ScrollViewReader and the scrolling does not work since iOS16.0.

In 16.0, it seems to work when I added Spacer after ScrollVIew {} within ScrollViewReader

ScrollViewReader {
    VStack {
        ScrollView {
              content
        }
        if #available(iOS 16.0, *) {
            Spacer()
        }
    }
}

In iOS16.1, the scrolling does not work again. This time, I managed to workaround by keeping the Spacer in the above code and updating the scroll anchor to ".top" (was using .bottom anchor before).

Over the past iOS versions, I had to adapt the anchor in order to get it to work for some versions. I hope Apple developer can be more consistent in making the scroll behaviour in future.

scroll.scrollTo(ANCHOR_NAME, anchor: .iOS15 ? .none : (.iOS16plus ? .top : .bottom))
extension Bool {
    static var iOS15: Bool {
        if #available(iOS 16, *) {
            return false
        } else if #available(iOS 15, *) {
            return true
        }
        return false
    }

    static var iOS16plus: Bool {
        guard #available(iOS 16, *) else {
            return false
        }
        return true
    }
}