首页 > 解决方案 > 拍摄视图屏幕截图崩溃 - UIViewControllerHierarchyInconsistency Only in ViewWillAppear

问题描述

我遇到了一个奇怪的问题,即当我使用以下代码在viewWillAppear应用程序崩溃 中截屏时

let renderr = UIGraphicsImageRenderer(size: self.view.bounds.size)
let image = renderr.image { (ctxt) in
    view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
 }

奇怪的是在viewDidLoadviewDidAppear它没有崩溃

以下是堆栈跟踪

  *** 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

什么是场景


从侧面菜单中,我点击了我的 Pick Button
-> 将其委托给 ViewController.Main
-> 在 Main ViewController 添加 ViewController.MyPickView 作为子 viewController
-> ViewController.MyPickView 有 containerView 有 ViewController.FavPic 作为 childViewController

注意:请不要将其标记为重复我见过很多类似错误的问题,但没有一个有帮助

任何帮助,将不胜感激

编辑代码

Step1 : 使用侧边菜单设置主控制器。我的问题中的完整代码链接 https://codereview.stackexchange.com/questions/209805/side-menu-handling-ios

第2步:现在当用户从侧面菜单中选择MyPicaddChildVC选项时,方法被调用

    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的容器视图

标签: iosswiftclass-hierarchychildviewcontroller

解决方案


推荐阅读