ScrollViewReader's scrollTo may be broken on iOS 15

ScrollViewReader's scrollTo scrolls too much in iOS 15. I had no problem with iOS 14.

This is the code:

import SwiftUI

struct ContentView: View {
  var body: some View {
    ScrollViewReader { proxy in
      ScrollView {
        VStack {
          Color.yellow
            .frame(height: 800)

          ScrollButton(proxy: proxy)
        }
      }
    }
  }
}

struct ScrollButton: View {
  let proxy: ScrollViewProxy

  @Namespace var bottomId

  var body: some View {
    VStack {
      Button("Scroll") {
        withAnimation {
          proxy.scrollTo(bottomId)
        }
      }
      Color.red
        .frame(height: 500)
        .id(bottomId)
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

I am facing scrolling issue while applying accessibility.

:
:
:
.modifier(if: adjustableAccessibilityScrollView) {

                        $0.accessibilityElement()

                            .accessibilityLabel(Text(viewModel.accessibilityFormLabel))

                            .accessibilityValue(Text(String(viewModel.chips[selectedChipIndex].text)))

                            .accessibilityAdjustableAction { direction in

                                switch direction {

                                case .increment:

                                    guard viewModel.chips.count - 1 > selectedChipIndex else { break }

                                    selectedChipIndex += 1

                                    viewModel.selectChip(index: selectedChipIndex)

                                    proxy.scrollTo(selectedChipIndex, anchor: .center)

                                case .decrement:

                                    guard selectedChipIndex > 0 else { break }

                                    selectedChipIndex -= 1

                                    viewModel.selectChip(index: selectedChipIndex)

                                    proxy.scrollTo(selectedChipIndex, anchor: .center)

                                @unknown default:

                                    break

                                }

                            }

                            .accessibilityAction(.default) {

                                viewModel.selectChip(index: selectedChipIndex)

                            }

                    }

I think I encountered the same issue today using Xcode 13.3 and iOS 15.4, and from what I can see the issue still persists. The following code reproduces the problem I'm having:

struct ContentView: View {
    var body: some View {
        ScrollViewReader { proxy in
            ScrollView {
                Spacer(minLength: 300)

                Button("Scroll to #50") {
                    proxy.scrollTo(50, anchor: .center)
                }

                VStack(spacing: 0) {
                    ForEach(0..<100) { i in
                        Text("\(i)")
                            .frame(height: 100)
                            .id(i)
                            .frame(maxWidth: .infinity)
                    }
                }
            }
        }
    }
}

I submitted a feedback to Apple using the above code sample: FB9959257.

Hi! I fixed the issue by adding

 DispatchQueue.async {
      ...
      proxy.scrollTo("id")
}

now scrollTo works perfect.

For me setting the background as a clear colour with id solved the issue:

ScrollViewReader { scrollViewProxy in
    ScrollView {
      LazyVStack {
        ForEach(viewModel.items) { item in
          ItemView(item)
            .background(Color.clear.id(item.id))

        }
      }
    }
  }

如何兼容iOS15是一个头疼的问题

ScrollViewReader's scrollTo may be broken on iOS 15
 
 
Q