我面临一个奇怪的问题,那就是当我使用以下代码在 viewWillAppear 应用崩溃时进行抓屏
let renderr = UIGraphicsImageRenderer(size: self.view.bounds.size)
let image = renderr.image { (ctxt) in
view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
}
奇怪的东西在 viewDidLoad 和 viewDidAppear 中,它没有崩溃
以下是堆栈跟踪
*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<App.FavPicVC: 0x7fcde2557bc0> should have parent view controller:(null) but actual parent is:<App.MyPicsVC: 0x7fcde26065b0>'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111e5a29b __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000110e4c735 objc_exception_throw + 48
2 CoreFoundation 0x0000000111e5a0f5 +[NSException raise:format:] + 197
3 UIKitCore 0x0000000117ca4749 -[UIView(Hierarchy) _associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 272
4 UIKitCore 0x0000000117ca4b4d -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 256
5 UIKitCore 0x0000000117cb63ce -[UIView(Internal) _addSubview:positioned:relativeTo:] + 456
6 UIKitCore 0x000000011753e27f _moveViewToTemporaryWindow + 496
7 UIKitCore 0x000000011753dc58 _UIDrawViewRectAfterCommit + 173
8 UIKitCore 0x0000000117c99e88 -[UIView drawViewHierarchyInRect:afterScreenUpdates:] + 33
9 App 0x000000010fd59b1f $S19App8FavPicVCC14viewWillAppearyySbFySo30UIGraphicsImageRendererContextCXEfU_ + 655
10 App 0x000000010fd59bad $S19App8FavPicVCC14viewWillAppearyySbFySo30UIGraphicsImageRendererContextCXEfU_TA + 13
11 App 0x000000010fd59bbc $SSo30UIGraphicsImageRendererContextCIgg_ABIegg_TR + 12
12 App 0x000000010fd59c01 $SSo30UIGraphicsImageRendererContextCIgg_ABIegg_TRTA + 17
13 App 0x000000010fd59c48 $SSo30UIGraphicsImageRendererContextCIegg_ABIyBy_TR + 56
14 UIKitCore 0x00000001177b5155 -[UIGraphicsRenderer runDrawingActions:completionActions:format:error:] + 409
15 UIKitCore 0x00000001177b4f8e -[UIGraphicsRenderer runDrawingActions:completionActions:error:] + 104
16 UIKitCore 0x00000001177b33b5 -[UIGraphicsImageRenderer imageWithActions:] + 163
17 App 0x000000010fd59754 $S19App8FavPicVCC14viewWillAppearyySbF + 868
18 App 0x000000010fd59d23 $S19App8FavPicVCC14viewWillAppearyySbFTo + 51
19 UIKitCore 0x0000000117cd86e3 -[UIViewController _setViewAppearState:isAnimating:] + 690
20 UIKitCore 0x0000000117cd8c49 __52-[UIViewController _setViewAppearState:isAnimating:]_block_invoke + 265
21 CoreFoundation 0x0000000111edc8b3 -[__NSArrayI enumerateObjectsWithOptions:usingBlock:] + 163
22 UIKitCore 0x0000000117cd89d7 -[UIViewController _setViewAppearState:isAnimating:] + 1446
23 UIKitCore 0x0000000117cd8e74 -[UIViewController __viewWillAppear:] + 131
24 UIKitCore 0x0000000117cda68d -[UIViewController viewWillMoveToWindow:] + 696
25 UIKitCore 0x0000000117ca4cdc -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] + 655
26 UIKitCore 0x0000000117cb63ce -[UIView(Internal) _addSubview:positioned:relativeTo:] + 456
27 App 0x000000010fd9e617 $S19App10SideMenuVCC08addChildF033_B2AA86EDE92945C8C278D22E699991A7LLyySo16UIViewControllerCF + 1031
28 App 0x000000010fd9fa23 $S19App10SideMenuVCC10itemTapped0G05indexyAA0dE8ListItemV_SitF + 83
29 App 0x000000010fd9fa69 $S19App10SideMenuVCCAA0dE18ItemTappedDelegateA2aDP04itemH00J05indexyAA0de4ListG0V_SitFTW + 9
30 App 0x000000010fd9d104 $S19App14SideMenuListVCC9tableView_14didSelectRowAtySo07UITableI0C_10Foundation9IndexPathVtF + 468
31 App 0x000000010fd9d80b $S19App14SideMenuListVCC9tableView_14didSelectRowAtySo07UITableI0C_10Foundation9IndexPathVtFTo + 107
32 UIKitCore 0x0000000117af8d29 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1813
33 UIKitCore 0x0000000117af8f44 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344
34 UIKitCore 0x00000001175af97e _runAfterCACommitDeferredBlocks + 318
35 UIKitCore 0x000000011759e424 _cleanUpAfterCAFlushAndRunDeferredBlocks + 384
36 UIKitCore 0x00000001175cd870 _afterCACommitHandler + 135
37 CoreFoundation 0x0000000111dbd037 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
38 CoreFoundation 0x0000000111db74ce __CFRunLoopDoObservers + 430
39 CoreFoundation 0x0000000111db7b61 __CFRunLoopRun + 1537
40 CoreFoundation 0x0000000111db7221 CFRunLoopRunSpecific + 625
41 GraphicsServices 0x000000011b4a31dd GSEventRunModal + 62
42 UIKitCore 0x00000001175a4115 UIApplicationMain + 140
43 App 0x000000010fdaac17 main + 71
44 libdyld.dylib 0x00000001152ab551 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
什么是视图层次结构
ViewController.Main.View-> ViewController.MyPickView.View-> ViewController.FavPic.View
什么是场景
我从侧面菜单中点击了我的选择按钮
->将其委托给ViewController.Main
-> Main ViewController中,将ViewController.MyPickView添加为子viewController
-> ViewController.MyPickView具有containerView,其中ViewView.FavPic作为childViewController
注意:请不要将其标记为重复项,因为我已经看到很多问题,但都存在类似的错误,但是没有一个帮助
任何帮助将不胜感激
编辑代码
步骤1:通过侧面菜单设置主控制器。链接https://codereview.stackexchange.com/questions/209805/side-menu-handling-ios
中我的问题中有完整代码 Step2:现在,当用户从侧面菜单addChildVC
中选择 MyPic 选项时,该方法称为
private func addChildVC (_ vc:UIViewController) {
self.removeAllChild()
self.addChild(vc)
vc.view.frame = self.view.bounds
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
self.currentCenterViewController = vc
}
private func removeAllChild() {
self.children.forEach{$0.willMove(toParent: nil);$0.view.removeFromSuperview();$0.removeFromParent()}
}
MYPic 视图控制器具有嵌入了 FavPic
的容器视图