我希望有人能在这里帮助我。一个星期以来,我在我的应用程序中看到了这个异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'index out of bounds for arranged subview: index = 5 expected to be less than or equal to 4'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3cf0e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50ba89b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e3cd4c +[NSException raise:format:] + 188
3 UIKitCore 0x00007fff49095fa8 -[UIStackView insertArrangedSubview:atIndex:] + 161
4 UIKitCore 0x00007fff48282ac1 -[_UIButtonBar _layoutBar] + 3461
5 UIKitCore 0x00007fff4828637e -[_UIButtonBarStackView updateConstraints] + 48
6 UIKitCore 0x00007fff490b0afa -[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 489
7 UIKitCore 0x00007fff490b10c8 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1187
8 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
9 Foundation 0x00007fff25aa5778 -[NSISEngine withBehaviors:performModifications:] + 84
10 UIKitCore 0x00007fff490b12de -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 112
11 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
12 Foundation 0x00007fff25aa5778 -[NSISEngine withBehaviors:performModifications:] + 84
13 UIKitCore 0x00007fff490b12de -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 112
14 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
15 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
16 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
17 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
18 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
19 Foundation 0x00007fff25aa5778 -[NSISEngine withBehaviors:performModifications:] + 84
20 UIKitCore 0x00007fff490b12de -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 112
21 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
22 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
23 UIKitCore 0x00007fff490b0f62 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 829
24 Foundation 0x00007fff25aa5778 -[NSISEngine withBehaviors:performModifications:] + 84
25 UIKitCore 0x00007fff490b186a __100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 85
26 UIKitCore 0x00007fff490b0077 -[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
27 UIKitCore 0x00007fff490b13c5 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 154
28 UIKitCore 0x00007fff490a1390 -[UIWindow(UIConstraintBasedLayout) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 92
29 UIKitCore 0x00007fff490b22e4 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 393
30 UIKitCore 0x00007fff4917f21b -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 272
31 UIKitCore 0x00007fff49193678 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
32 QuartzCore 0x00007fff2b4c6398 -[CALayer layoutSublayers] + 255
33 QuartzCore 0x00007fff2b4cc523 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 523
34 QuartzCore 0x00007fff2b4d7bba _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
35 QuartzCore 0x00007fff2b420c04 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
36 QuartzCore 0x00007fff2b4545ef _ZN2CA11Transaction6commitEv + 649
37 QuartzCore 0x00007fff2b454f81 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 79
38 CoreFoundation 0x00007fff23da0127 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
39 CoreFoundation 0x00007fff23d9abde __CFRunLoopDoObservers + 430
40 CoreFoundation 0x00007fff23d9b12a __CFRunLoopRun + 1226
41 CoreFoundation 0x00007fff23d9a944 CFRunLoopRunSpecific + 404
42 GraphicsServices 0x00007fff38ba6c1a GSEventRunModal + 139
43 UIKitCore 0x00007fff48c8b9ec UIApplicationMain + 1605
44 MyApp 0x00000001012dd63b main + 75
45 libdyld.dylib 0x00007fff51a231fd start + 1
46 ??? 0x0000000000000001 0x0 + 1
)
我找不到它的根本原因。因为提到了
_UIButtonBarStackView
,所以我认为它与UINavigationBar
UIBarButton
有关。当我驳回模态呈现的UINavigationController
时似乎会发生,但我不确定。
它看起来也开始发生在 iOS 13.4.1 上,因为我找不到任何旧版本的崩溃报告。
有人可能知道这里发生了什么吗?
找出原因:我再次将相同的 UIBarButtonItem 添加到
navigationItem.rightBarButtonItems
。自 iOS 13.4.1(-ish) 以来,Apple 似乎对此提出了例外。
就我而言,我使用的是SwiftUI 问题是由于使用添加 2 个不同的
ToolbarItem
共享相同的属性id
导致问题的旧代码
.toolbar {
ToolbarItem(id: "bookmark", placement: ToolbarItemPlacement.navigationBarTrailing, showsByDefault: true) {
Image(systemName: "bookmark")
.foregroundColor(AppColors.skyBlueColor)
}
//this was the issue duplicated ids
ToolbarItem(id: "bookmark", placement: ToolbarItemPlacement.navigationBarTrailing, showsByDefault: true) {
Image(systemName: "square.and.arrow.up")
.foregroundColor(AppColors.skyBlueColor)
}
}
所以修复是为
id
提供独特的ToolbarItem