iOS10及更早版本App崩溃:无法实例化_UIScrollViewLayoutGuide求排查方案
针对旧设备上
NSInvalidUnarchiveOperationException错误的排查方案 嘿,这个问题我之前帮同事排查过好几次,先给你拆解下核心原因:从日志里的Could not instantiate class named _UIScrollViewLayoutGuide能直接看出来,_UIScrollViewLayoutGuide是iOS 11才引入的私有系统类,你的旧设备系统版本肯定低于iOS 11,所以加载界面时才会触发崩溃。下面给你一步步的排查和解决思路:
一、先验证核心前提:确认旧设备的iOS版本
- 直接打开旧设备的「设置」→「通用」→「关于本机」,查看iOS版本号。如果是10.x及以下,那完全匹配这个错误的触发条件——低版本系统根本不认识这个类。
二、排查界面文件(XIB/Storyboard)的问题
从调用栈能看到崩溃是在加载HomeMainViewController的视图时触发的,重点检查这个界面里的UIScrollView相关设置:
- 打开对应的XIB/Storyboard,选中ScrollView,查看右侧「Size Inspector」里的「Content Layout Guide」和「Frame Layout Guide」是否被勾选启用。这两个是Xcode针对iOS11+提供的可视化约束工具,一旦启用,低版本系统解析XIB时就会找不到对应的
_UIScrollViewLayoutGuide类,直接崩溃。
三、具体修复方案
方案1:修改XIB/Storyboard为兼容写法
- 取消ScrollView的「Content Layout Guide」和「Frame Layout Guide」勾选;
- 给ScrollView添加一个子视图(比如UIView,作为contentView);
- 给contentView添加约束:top、leading、trailing、bottom都和ScrollView的边缘对齐,同时设置contentView的width等于ScrollView的width(横向滚动场景)或者height等于ScrollView的height(纵向滚动场景);
- 之后所有需要放在ScrollView里的内容都加到这个contentView上,这样就能完美替代Layout Guide的作用,同时兼容iOS11以下系统。
方案2:调整项目的Deployment Target
- 打开项目设置,找到「Deployment Info」里的「iOS Deployment Target」,把版本降到你需要兼容的最低版本(比如iOS9.0);
- 注意:这一步只是确保项目编译时会考虑低版本系统的兼容性,但如果已经用了iOS11+的特性(比如Layout Guide),还是需要配合方案1修改界面。
方案3:代码动态适配(适合不想修改XIB的场景)
在HomeMainViewController的viewDidLoad方法里,添加系统版本判断,动态替换约束:
- (void)viewDidLoad { [super viewDidLoad]; if (@available(iOS 11.0, *)) { // iOS11及以上,保留原有Layout Guide约束 } else { // 低版本系统,移除原有Layout Guide相关约束,手动添加兼容约束 UIView *contentView = [[UIView alloc] init]; contentView.translatesAutoresizingMaskIntoConstraints = NO; [self.scrollView addSubview:contentView]; // 添加contentView和scrollView的约束 [NSLayoutConstraint activateConstraints:@[ contentView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor, contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.leadingAnchor, contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor, contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor, contentView.widthAnchor constraintEqualToAnchor:self.scrollView.widthAnchor // 横向滚动用这个,纵向滚动换成heightAnchor ]]; // 把原来放在Layout Guide上的子视图移到contentView上 // ... } }
四、额外验证步骤
- 修改完成后,用Xcode的「模拟器」选择低版本iOS(比如iOS10.3)运行,看看是否还会崩溃;
- 如果还是有问题,检查其他界面里的ScrollView是否也用了Layout Guide,因为可能不止这一个界面存在兼容问题。
附上你提供的错误日志,方便后续参考:
2019-12-30 16:52:04.784 Hospital[7075:164758] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UIScrollViewLayoutGuide' *** First throw call stack: ( 0 CoreFoundation 0x000000010ad9fb0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010a804141 objc_exception_throw + 48 2 CoreFoundation 0x000000010ae08625 +[NSException raise:format:] + 197 3 UIKit 0x000000010c15368d UINibDecoderDecodeObjectForValue + 323 4 UIKit 0x000000010c15353f -[UINibDecoder decodeObjectForKey:] + 89 5 Foundation 0x0000000109b0e417 -[NSLayoutConstraint initWithCoder:] + 757 6 UIKit 0x000000010c1537ff UINibDecoderDecodeObjectForValue + 693 7 UIKit 0x000000010c15399d UINibDecoderDecodeObjectForValue + 1107 8 UIKit 0x000000010c15353f -[UINibDecoder decodeObjectForKey:] + 89 9 UIKit 0x000000010bbee9de -[UIView initWithCoder:] + 2209 10 UIKit 0x000000010bc29687 -[UIScrollView initWithCoder:] + 71 11 UIKit 0x000000010c1537ff UINibDecoderDecodeObjectForValue + 693 12 UIKit 0x000000010c15399d UINibDecoderDecodeObjectForValue + 1107 13 UIKit 0x000000010c15353f -[UINibDecoder decodeObjectForKey:] + 89 14 UIKit 0x000000010bbee4ed -[UIView initWithCoder:] + 944 15 UIKit 0x000000010c1537ff UINibDecoderDecodeObjectForValue + 693 16 UIKit 0x000000010c15353f -[UINibDecoder decodeObjectForKey:] + 89 17 UIKit 0x000000010bf62f70 -[UIRuntimeConnection initWithCoder:] + 178 18 UIKit 0x000000010c1537ff UINibDecoderDecodeObjectForValue + 693 19 UIKit 0x000000010c15399d UINibDecoderDecodeObjectForValue + 1107 20 UIKit 0x000000010c15353f -[UINibDecoder a:] + 89 21 UIKit 0x000000010bf6216d -[UINib instantiateWithOwner:options:] + 1262 22 UIKit 0x000000010bcfcfc3 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 23 UIKit 0x000000010bcfd8d9 -[UIViewController loadView] + 177 24 UIKit 0x000000010bcfdc0a -[UIViewController loadViewIfRequired] + 195 25 UIKit 0x000000010bcfe45a -[UIViewController view] + 27 26 Hospital 0x00000001086ecb00 $s15_Hospital15PagerScrollViewC03setcdE010toParentVC05pagesI0ySo16UIViewControllerC_SayAHGSgtF + 1152 27 Hospital 0x00000001086f6b85 $s15_Hospital22HomeMainViewControllerC11viewDidLoadyyF + 3861 28 Hospital 0x00000001086f6e9b $s15_Hospital22HomeMainViewControllerC11viewDidLoadyyFTo + 43 29 UIKit 0x000000010bcfe01a -[UIViewController loadViewIfRequired] + 1235 30 UIKit 0x000000010bd3ce6c -[UINavigationController _layoutViewController:] + 56 31 UIKit 0x000000010bd3d74a -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 466 32 UIKit 0x000000010bd3d8bb -[UINavigationController _startTransition:fromViewController:toViewController:] + 127 33 UIKit 0x000000010bd3ea03 -[UINavigationController _startDeferredTransitionIfNeeded:] + 843 34 UIKit 0x000000010bd3fb41 -[UINavigationController __viewWillLayoutSubviews] + 58 35 UIKit 0x000000010bf3160c -[UILayoutContainerView layoutSubviews] + 231 36 UIKit 0x000000010bc1e55b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268 37 QuartzCore 0x000000010a572904 -[CALayer layoutSublayers] + 146 38 QuartzCore 0x000000010a566526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370 39 QuartzCore 0x000000010a5663a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 40 QuartzCore 0x000000010a4f5e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 41 QuartzCore 0x000000010a522130 _ZN2CA11Transaction6commitEv + 468 42 QuartzCore 0x000000010a522b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 43 CoreFoundation 0x000000010ad45717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 44 CoreFoundation 0x000000010ad45687 __CFRunLoopDoObservers + 391 45 CoreFoundation 0x000000010ad2a038 CFRunLoopRunSpecific + 440 46 UIKit 0x000000010bb5508f -[UIApplication _run] + 468 47 UIKit 0x000000010bb5b134 UIApplicationMain + 159 48 Hospital 0x000000010872773b main + 75 49 libdyld.dylib 0x000000010e5d965d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
内容的提问来源于stack exchange,提问作者Jaseel.Dev
相关产品推荐
相关产品推荐

