【问题标题】:SwiftUI: Crash when calling scrollTo method with anchor: .centerSwiftUI:使用锚点调用 scrollTo 方法时崩溃:.center
【发布时间】:2021-01-26 06:20:06
【问题描述】:

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

我的观点是这样的:

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)
            }
        }
    }
}

崩溃堆栈如下所示:

Crashed: com.apple.main-thread
0  AttributeGraph                 0x1cedce974 AGGraphGetValue + 284
1  SwiftUI                        0x1addcbe5c _ViewCache.layout.getter + 52
2  SwiftUI                        0x1addcc2f4 _ViewCache.withPlacementData<A>(_:) + 160
3  SwiftUI                        0x1addd02d0 closure #1 in IncrementalScrollable.makeTarget(at:anchor:) + 336
4  SwiftUI                        0x1addd6a9c partial apply for closure #1 in IncrementalScrollable.makeTarget(at:anchor:) + 56
5  SwiftUI                        0x1ae2241fc HostingScrollView.updateAnimationTarget(_:) + 216
6  SwiftUI                        0x1ae2245c4 HostingScrollView.bounds.didset + 188
7  SwiftUI                        0x1ae2244e8 HostingScrollView.bounds.setter + 128
8  SwiftUI                        0x1ae22444c @objc HostingScrollView.bounds.setter + 44
9  UIKitCore                      0x1aa494e4c -[UIScrollView setContentOffset:] + 804
10 UIKitCore                      0x1aa48f1e0 -[UIScrollViewScrollAnimation setProgress:] + 320
11 UIKitCore                      0x1a95094b8 -[UIAnimator _advanceAnimationsOfType:withTimestamp:] + 276
12 QuartzCore                     0x1aa8e46a4 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 664
13 QuartzCore                     0x1aa9bb1a0 display_timer_callback(__CFMachPort*, void*, long, void*) + 280
14 CoreFoundation                 0x1a7633ce4 __CFMachPortPerform + 176
15 CoreFoundation                 0x1a7658098 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 60
16 CoreFoundation                 0x1a7657440 __CFRunLoopDoSource1 + 596
17 CoreFoundation                 0x1a7651320 __CFRunLoopRun + 2360
18 CoreFoundation                 0x1a76504bc CFRunLoopRunSpecific + 600
19 GraphicsServices               0x1be0d5820 GSEventRunModal + 164
20 UIKitCore                      0x1a9ff4734 -[UIApplication _run] + 1072
21 UIKitCore                      0x1a9ff9e10 UIApplicationMain + 168
22 Fablur                         0x102453f8c main + 12 (AppDelegate.swift:12)
23 libdyld.dylib                  0x1a7317e60 start + 4

【问题讨论】:

  • 有同样的问题。我正在使用 scrollTo ,如果我删除锚它会停止崩溃。但是,我的崩溃仅在视图消失时发生

标签: swiftui scrollview


【解决方案1】:

我发现如果你使用 scrollTo 和锚 并将滚动视图的内容定位到它通常无法通过正常滚动到达的位置,那么它会崩溃视图被移除。

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

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

【讨论】:

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

删除 withAnimation 块,你会没事的

.onChange(of: mo.scrollToIdx) { idx in
    scrollProxy.scrollTo(targetIdx, anchor: .center)
}

【讨论】:

  • 这对我有用。谢谢。
【解决方案3】:

我遇到了非常类似的问题,并得出了(潜在的)结论,即 ScrollViewReader 无法正确计算,因为您的内容位于 LazyVStack 中,并且由于 Lazy 项目没有预加载......读者没有知道滚动视图实际上有多长。至少这是我的猜测......

【讨论】:

    【解决方案4】:

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

    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)
                       }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢,这可能是原因 - 我现在很难重现崩溃,所以会检查更多; isShowingContent 的原因也来自这里:stackoverflow.com/a/61765994/1032900 以防万一这有助于更多地挖掘问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多