Hello,
I found that ScrollViewProxy.scrollTo(_:anchor:)
always crash on iOS 16 beta 1-7. As soon as the Section
in the List
changes, the ScrollViewProxy
does not scroll properly to the specified cell. This issue only appeared on iOS 16.
Sample Code (for Xcode 14)
import SwiftUI
struct ContentView: View {
@State private var hideSection1 = false
var body: some View {
ScrollViewReader { scrollView in
List {
if !hideSection1 {
Section(header: Text("Section 1")) {
ForEach(1...10, id: \.self) { i in
Text("\(i)")
}
}
}
Section(header: Text("Section 2")) {
ForEach(11...20, id: \.self) { i in
Text("\(i)")
}
}
}
.safeAreaInset(edge: .bottom, spacing: 0) {
VStack {
// Crash Steps:
// 1. Tap "Go to 20", OK ✅
// 2. Hide Section 1
// 3. Tap "Go to 20" again <--- ☠️ crash
Button("Go to 20") {
withAnimation {
scrollView.scrollTo(20, anchor: .center)
}
}
Toggle("Hide Section 1", isOn: $hideSection1)
}
.padding()
.background {
Rectangle()
.fill(.ultraThinMaterial)
.ignoresSafeArea()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Crash Message
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds section (1) when there are only 1 sections. Collection view: <SwiftUI.UpdateCoalescingCollectionView: 0x13c88cc00; baseClass = UICollectionView; frame = (0 0; 390 844); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000323fc60>; backgroundColor = <UIDynamicSystemColor: 0x600002922dc0; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x600003c2f260>; contentOffset: {0, -47}; contentSize: {390, 498.66666666666669}; adjustedContentInset: {47, 0, 129.66666666666674, 0}; layout: <UICollectionViewCompositionalLayout: 0x13c510ed0>; dataSource: <_TtGC7SwiftUI31UICollectionViewListCoordinatorGOS_19SelectionManagerBoxOs5Never__: 0x13c513670>>.'