热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

多次PushPop导致的`Can'taddselfassubview`问题

崩溃堆栈***TerminatingappduetouncaughtexceptionNSInvalidArgumentException,reason:Cantaddselfas
  • 崩溃堆栈

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
*** First throw call stack:
(0 CoreFoundation 0x000000010ec2df35 __exceptionPreprocess + 1651 libobjc.A.dylib 0x000000010e8c8bb7 objc_exception_throw + 452 CoreFoundation 0x000000010ec2de6d +[NSException raise:format:] + 2053 UIKit 0x000000010b351982 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1234 UIKit 0x000000010b2d0b19 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 18145 UIKit 0x000000010b34c5ce +[UIView(Animation) performWithoutAnimation:] + 656 UIKit 0x000000010b2d0072 -[_UINavigationParallaxTransition animateTransition:] + 12257 UIKit 0x000000010b424e6c -[UINavigationController _startCustomTransition:] + 30388 UIKit 0x000000010b4303fe -[UINavigationController _startDeferredTransitionIfNeeded:] + 3869 UIKit 0x000000010b430f47 -[UINavigationController __viewWillLayoutSubviews] + 4310 UIKit 0x000000010b576509 -[UILayoutContainerView layoutSubviews] + 20211 UIKit 0x000000010b354973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 52112 QuartzCore 0x000000010f589de8 -[CALayer layoutSublayers] + 15013 QuartzCore 0x000000010f57ea0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 38014 QuartzCore 0x000000010f57e87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 2415 QuartzCore 0x000000010f4ec63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 24216 QuartzCore 0x000000010f4ed74a _ZN2CA11Transaction6commitEv + 39017 UIKit 0x000000010b2d814d _UIApplicationHandleEventQueue + 203518 CoreFoundation 0x000000010eb63551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 1719 CoreFoundation 0x000000010eb5941d __CFRunLoopDoSources0 + 26920 CoreFoundation 0x000000010eb58a54 __CFRunLoopRun + 86821 CoreFoundation 0x000000010eb58486 CFRunLoopRunSpecific + 47022 GraphicsServices 0x0000000110c659f0 GSEventRunModal + 16123 UIKit 0x000000010b2db420 UIApplicationMain + 128224 moma-beta 0x00000001071abe1f main + 11125 libdyld.dylib 0x000000011068f145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

  • 复现步骤
  • 修复方案: Hook UINavigationController

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;

两个方法。

  • Code

@implementation UINavigationController (PushPopCrashFix)+ (void)load {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{SwizzledMethod(self, self, @selector(popViewControllerAnimated:), @selector(p_PopViewControllerAnimated:));SwizzledMethod(self, self, @selector(pushViewController:animated:), @selector(p_PushViewController:animated:));});
}- (BOOL)prohibitPushPop {return [objc_getAssociatedObject(self, @selector(prohibitPushPop)) boolValue];
}- (void)setProhibitPushPop:(BOOL) prohibitPushPop {objc_setAssociatedObject(self, @selector(prohibitPushPop), @(prohibitPushPop), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (UIViewController *)p_FixPopViewControllerAnimated:(BOOL)animated {if (self.prohibitPushPop) {return nil;}self.prohibitPushPop = YES;UIViewController *vc = [self DPPFixPopViewControllerAnimated:animated];[CATransaction setCompletionBlock:^{self.prohibitPushPop = NO;}];return vc;
}- (void)p_PushViewController:(UIViewController *)viewController animated:(BOOL)animated {if (self.prohibitPushPop) {return;}self.prohibitPushPop = YES;[self DPPFixPushViewController:viewController animated:animated];[CATransaction setCompletionBlock:^{self.prohibitPushPop = NO;}];
}@end



推荐阅读
author-avatar
豆伊一
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有