SwiftUI:使用锚点调用scrollTo方法时崩溃:.center

hyo*_*uuu 9 scrollview swiftui

不能 100% 确定它是否是由 引起的,因为anchor我刚刚删除了它并且到目前为止还没有经历过崩溃(如果发生更多崩溃,将会报告) - 有人可以检查并帮助看看这是否真的是错误所在吗?还要提交 Apple bug。

\n

我的观点是这样的:

\n
var body: some View {\n    ScrollViewReader { scrollProxy in\n        ScrollView {\n            if isShowingContent {\n                LazyVStack {\n                    ForEach(mo.msgMos.indices, id: \\.self) { i in\n                        ...\n                    }\n                }\n            }\n        }\n        .onChange(of: mo.scrollToIdx) { idx in\n            withAnimation {\n                scrollProxy.scrollTo(targetIdx, anchor: .center)\n            }\n        }\n    }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

而\xc2\xa0崩溃堆栈如下所示:

\n
Crashed: com.apple.main-thread\n0  AttributeGraph                 0x1cedce974 AGGraphGetValue + 284\n1  SwiftUI                        0x1addcbe5c _ViewCache.layout.getter + 52\n2  SwiftUI                        0x1addcc2f4 _ViewCache.withPlacementData<A>(_:) + 160\n3  SwiftUI                        0x1addd02d0 closure #1 in IncrementalScrollable.makeTarget(at:anchor:) + 336\n4  SwiftUI                        0x1addd6a9c partial apply for closure #1 in IncrementalScrollable.makeTarget(at:anchor:) + 56\n5  SwiftUI                        0x1ae2241fc HostingScrollView.updateAnimationTarget(_:) + 216\n6  SwiftUI                        0x1ae2245c4 HostingScrollView.bounds.didset + 188\n7  SwiftUI                        0x1ae2244e8 HostingScrollView.bounds.setter + 128\n8  SwiftUI                        0x1ae22444c @objc HostingScrollView.bounds.setter + 44\n9  UIKitCore                      0x1aa494e4c -[UIScrollView setContentOffset:] + 804\n10 UIKitCore                      0x1aa48f1e0 -[UIScrollViewScrollAnimation setProgress:] + 320\n11 UIKitCore                      0x1a95094b8 -[UIAnimator _advanceAnimationsOfType:withTimestamp:] + 276\n12 QuartzCore                     0x1aa8e46a4 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 664\n13 QuartzCore                     0x1aa9bb1a0 display_timer_callback(__CFMachPort*, void*, long, void*) + 280\n14 CoreFoundation                 0x1a7633ce4 __CFMachPortPerform + 176\n15 CoreFoundation                 0x1a7658098 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 60\n16 CoreFoundation                 0x1a7657440 __CFRunLoopDoSource1 + 596\n17 CoreFoundation                 0x1a7651320 __CFRunLoopRun + 2360\n18 CoreFoundation                 0x1a76504bc CFRunLoopRunSpecific + 600\n19 GraphicsServices               0x1be0d5820 GSEventRunModal + 164\n20 UIKitCore                      0x1a9ff4734 -[UIApplication _run] + 1072\n21 UIKitCore                      0x1a9ff9e10 UIApplicationMain + 168\n22 Fablur                         0x102453f8c main + 12 (AppDelegate.swift:12)\n23 libdyld.dylib                  0x1a7317e60 start + 4\n
Run Code Online (Sandbox Code Playgroud)\n

Mor*_*rgz 5

我发现,如果您使用带有锚点的scrollTo,并且它将滚动视图的内容定位到通常无法通过正常滚动到达的位置,那么当视图被删除时,它将崩溃。

例如。滚动到滚动视图中的最后一个视图并使用 .leading 或 .center 位置。

我通过在滚动视图的末尾添加一个空白视图来解决这个问题。不理想但有效。

  • 这对我也有用。我在水平方向使用“ScrollView”创建了一个选项卡列表。即使删除“withAnimation”,错误仍然存​​在。这绝对是一个内部 SwiftUI 问题,并且不会反复出现,因此很难重现和修复它,但在添加到滚动的项目之后添加一个 `Spacer()` 为我解决了这个问题。 (2认同)

Asp*_*eri 1

我认为它应该附加在不同的地方

var body: some View {
    ScrollViewReader { scrollProxy in
        ScrollView {
            if isShowingContent {
                LazyVStack {
                    ForEach(mo.msgMos.indices, id: \.self) { i in
                        ...
                    }
                }
                .onChange(of: mo.scrollToIdx) { idx in
                   withAnimation {
                      scrollProxy.scrollTo(targetIdx, anchor: .center)
                   }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)