触发键盘时SwiftUI崩溃

问题描述

我在 SwiftUI 中构建了一个应用程序,但遇到了一个我无法解决的问题。 我的应用程序包含一个主屏幕,用户可以在其中配置游戏,以及一个他们在开始游戏时进入的游戏屏幕。这两个都是 NavigationViews,一旦游戏开始,HomeScreen 就会通过 NavigationLink 链接到 GameScreen。在我看来,我经常使用 GeometryReader 来确定组件的大小。

在我的 GameView 上,我有一个包含 UIViewRepresentableUITextField。我已经实现了这个而不是 SwiftUI TextField,因为我想在视图出现时触发 becomeFirstResponder() 以获得更好的用户体验。但是,触发键盘会导致我的应用程序崩溃并显示以下堆栈跟踪:

Thread 0 name:
Thread 0 Crashed:
0   SwiftUI                         0x00000001b42194c8 _FlexFrameLayout.sizeThatFits(in:context:child:) + 676 (FrameLayout.swift:278)
1   SwiftUI                         0x00000001b4219630 protocol witness for UnaryLayout.sizeThatFits(in:context:child:) in conformance _FlexFrameLayout + 68 (<compiler-generated>:0)
2   SwiftUI                         0x00000001b4219630 protocol witness for UnaryLayout.sizeThatFits(in:context:child:) in conformance _FlexFrameLayout + 68 (<compiler-generated>:0)
3   SwiftUI                         0x00000001b4145a3c closure #1 in UnaryLayoutEngine.sizeThatFits(_:) + 76 (UnaryLayoutView.swift:258)
4   SwiftUI                         0x00000001b414597c UnaryLayoutEngine.sizeThatFits(_:) + 444 (StandardLibraryAdditions.swift:0)
5   SwiftUI                         0x00000001b43d0a20 LayoutComputer.EngineDelegate.sizeThatFits(_:) + 68 (LayoutComputer.swift:154)
6   SwiftUI                         0x00000001b43cf7f4 LayoutComputer.Delegate.lengthThatFits(_:in:) + 56 (LayoutComputer.swift:92)
7   SwiftUI                         0x00000001b43766f0 StackLayout.UnmanagedImplementation.prioritize(_:proposedSize:) + 1312 (LayoutComputer.swift:40)
8   SwiftUI                         0x00000001b4375c3c specialized StackLayout.UnmanagedImplementation.sizeChildrenGenerallyWithConcreteMajorProposal(in:minorProposalForChild:) + 152 (StackLayout.swift:582)
9   SwiftUI                         0x00000001b4376f68 specialized StackLayout.UnmanagedImplementation.placeChildren1(in:minorProposalForChild:) + 168 (StackLayout.swift:528)
10  SwiftUI                         0x00000001b4374604 StackLayout.UnmanagedImplementation.placeChildren(in:) + 184 (StackLayout.swift:800)
11  SwiftUI                         0x00000001b4374048 StackLayout.UnmanagedImplementation.sizeThatFits(_:) + 368 (StackLayout.swift:203)
12  SwiftUI                         0x00000001b4373ecc StackLayout.Storage.sizeThatFits(_:) + 68 (StackLayout.swift:158)
13  SwiftUI                         0x00000001b4142ac4 _PaddingLayout.sizeThatFits(in:context:child:) + 200 (LayoutComputer.swift:47)
14  SwiftUI                         0x00000001b4142fd8 protocol witness for UnaryLayout.sizeThatFits(in:context:child:) in conformance _PaddingLayout + 40 (<compiler-generated>:0)
15  SwiftUI                         0x00000001b4145a3c closure #1 in UnaryLayoutEngine.sizeThatFits(_:) + 76 (UnaryLayoutView.swift:258)
16  SwiftUI                         0x00000001b414597c UnaryLayoutEngine.sizeThatFits(_:) + 444 (StandardLibraryAdditions.swift:0)
17  SwiftUI                         0x00000001b43d0a20 LayoutComputer.EngineDelegate.sizeThatFits(_:) + 68 (LayoutComputer.swift:154)
18  SwiftUI                         0x00000001b3d1eeb0 specialized FrameLayoutCommon.commonPlacement(of:in:) + 180 (LayoutComputer.swift:47)
19  SwiftUI                         0x00000001b4219020 protocol witness for UnaryLayout.placement(of:in:) in conformance _FrameLayout + 56 (<compiler-generated>:0)
20  SwiftUI                         0x00000001b4145bd0 closure #1 in UnaryLayoutEngine.childplacement(at:) + 64 (UnaryLayoutView.swift:276)
21  SwiftUI                         0x00000001b4145198 UnaryLayoutEngine.childplacement(at:) + 212 (StandardLibraryAdditions.swift:498)
22  SwiftUI                         0x00000001b4145a90 UnaryLayoutEngine.explicitAlignment(_:at:) + 68 (UnaryLayoutView.swift:266)
23  SwiftUI                         0x00000001b4145bfc protocol witness for LayoutEngineProtocol.explicitAlignment(_:at:) in conformance UnaryLayoutEngine<A> + 12 (<compiler-generated>:0)
24  SwiftUI                         0x00000001b43d0ae0 LayoutComputer.EngineDelegate.explicitAlignment(_:at:) + 68 (LayoutComputer.swift:164)
25  SwiftUI                         0x00000001b410d0ec SecondaryLayerGeometryQuery.value.getter + 460 (LayoutComputer.swift:57)
26  SwiftUI                         0x00000001b3dc7904 specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32 (<compiler-generated>:0)
27  AttributeGraph                  0x00000001d533b14c AG::Graph::UpdateStack::update() + 484 (ag-closure.h:108)
28  AttributeGraph                  0x00000001d533b55c AG::Graph::update_attribute(AG::data::ptr<AG::Node>,bool) + 328 (ag-graph-update.cc:563)
29  AttributeGraph                  0x00000001d53440c4 AG::Subgraph::update(unsigned int) + 864 (ag-subgraph.cc:613)
30  SwiftUI                         0x00000001b45e1ef4 GraphHost.runTransaction() + 164 (GraphHost.swift:491)
31  SwiftUI                         0x00000001b45e4b94 GraphHost.runTransaction(_:) + 88 (GraphHost.swift:471)
32  SwiftUI                         0x00000001b45e3788 GraphHost.flushTransactions() + 172 (GraphHost.swift:459)
33  SwiftUI                         0x00000001b454ad64 closure #1 in VieWrendererHost.render(interval:updatedisplayList:) + 352 (VieWrendererHost.swift:167)
34  SwiftUI                         0x00000001b4543470 VieWrendererHost.render(interval:updatedisplayList:) + 316 (VieWrendererHost.swift:160)
35  SwiftUI                         0x00000001b469eff8 _UIHostingView.displayLinkTimer(timestamp:) + 156 (UIHostingView.swift:1561)
36  SwiftUI                         0x00000001b4063f58 displayLink.displayLinkTimer(_:) + 88 (_UIHostingView.swift:468)
37  SwiftUI                         0x00000001b4063fac @objc displayLink.displayLinkTimer(_:) + 40 (<compiler-generated>:0)
38  QuartzCore                      0x00000001b0cbd640 CA::display::displayLink::dispatch_items(unsigned long long,unsigned long long,unsigned long long) + 660 (CAdisplay.mm:2635)
39  QuartzCore                      0x00000001b0d8fa20 display_timer_callback(__CFMachPort*,void*,long,void*) + 276 (CAdisplayTimer.cpp:166)
40  CoreFoundation                  0x00000001adae2b54 __CFMachPortPerform + 172 (CFMachPort.c:537)
41  CoreFoundation                  0x00000001adb06420 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56 (CFRunLoop.c:1991)
42  CoreFoundation                  0x00000001adb05834 __CFRunLoopDoSource1 + 588 (CFRunLoop.c:2131)
43  CoreFoundation                  0x00000001adaff9f4 __CFRunLoopRun + 2332 (CFRunLoop.c:3146)
44  CoreFoundation                  0x00000001adafeba0 CFRunLoopRunSpecific + 572 (CFRunLoop.c:3242)
45  GraphicsServices                0x00000001c4867598 GSEventRunModal + 160 (GSEvent.c:2259)
46  UIKitCore                       0x00000001b03f02f4 -[UIApplication _run] + 1052 (UIApplication.m:3253)
47  UIKitCore                       0x00000001b03f5874 UIApplicationMain + 164 (UIApplication.m:4707)
48  Darts                           0x000000010030488c main + 68 (DSGame.swift:20)
49  libdyld.dylib                   0x00000001ad7dd568 start + 4

我确定是键盘导致了这次崩溃,因为当我在显示视图时删除 becomeFirstResponder() 时,崩溃消失了。当我点击文本字段以手动触发键盘时,我的应用程序崩溃了。同样在我的应用程序中触发键盘的其他区域,我的应用程序因此堆栈跟踪而崩溃。

{{3}} 线程表明我的问题是由 SwiftUI 中的错误引起的,但我无法确定,因为该问题包含的上下文太少。

似乎发生的事情是,当键盘出现时,视图的可用空间不仅会针对当前位于导航堆栈顶部的视图进行调整,还会针对其他位于后台的视图和当前不可见。

我想补充一点,设备电源似乎会影响这个问题:在 iPhone 12 Pro 上,这种情况仅在几周后开始发生,而在 iPhone 7 或 8 上,它也会在首次发布时发生。

问题 我很好奇我在这里做错了什么。其他人如何处理 SwiftUI 中出现的键盘?这确实是 SwiftUI 错误吗?如果是,是否有解决方法

非常感谢有关此问题的任何指导,因为此问题使我的应用无法使用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)