我正在开发一个使用UITextField子类的旧应用程序(让我们称之为CustomTextField),它使用xibs / storyboards在iOS 8.4中显示。但是,iOS 9中的两个视图中的UI层次结构中缺少它。
第一个失败案例:UITableViewCell子类有一个通过故事板连接的CustomTextField出口。表视图的视图控制器使用instantiateViewControllerWithIdentifier:
进行实例化。它的tableView:cellForRowAtIndexPath:
方法返回一个非零单元格,但CustomTextField与自定义单元格的其他元素(开关,图像视图和标签)一起缺失。
第二个失败案例:在主视图控制器的viewDidLoad
中,CustomTextField及其superview使用类别方法loadInstanceFromNib
通过xib添加,但CustomTextField从UI层次结构:
+ (id)loadInstanceFromNib
{
UIView *result;
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id anObject in elements) {
if ([anObject isKindOfClass:[self class]]) {
result = anObject;
break;
}
}
return result;
}
我在CustomTextField.m中放了几个断点。在iOS 9和8.4中,都会调用awakeFromNib
。但是,willMoveToSuperview
仅在8.4中针对两个失败案例调用。这是willMoveToSuperView
的回溯:
* thread #1: tid = 0xde22fd, 0x00088ca9 Shop`-[CustomTextField willMoveToSuperview:](self=0x7be75690, _cmd=0x02115c1c, newSuperview=0x7be75160) + 57 at CustomTextField.m:39, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x00088ca9 Shop`-[CustomTextField willMoveToSuperview:](self=0x7be75690, _cmd=0x02115c1c, newSuperview=0x7be75160) + 57 at CustomTextField.m:39
frame #1: 0x0188be77 UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 622
frame #2: 0x0187e976 UIKit`-[UIView(Hierarchy) addSubview:] + 56
frame #3: 0x0186d87e UIKit`-[UIView _setTraitStorageSubviews:] + 647
frame #4: 0x010ca608 Foundation`_NSSetUsingKeyValueSetter + 115
frame #5: 0x010ca58d Foundation`-[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
frame #6: 0x0188f13f UIKit`-[UIView(CALayerDelegate) setValue:forKey:] + 168
frame #7: 0x01100016 Foundation`-[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 386
frame #8: 0x01ab4d0e UIKit`-[_UIRelationshipTraitStorage applyRecordsMatchingTraitCollection:] + 1256
frame #9: 0x01ab3a89 UIKit`-[NSObject(_UITraitStorageAccessors) _applyTraitStorageRecordsForTraitCollection:] + 202
frame #10: 0x01871f84 UIKit`-[UIView _traitCollectionDidChangeFromOldCollection:toNewCollection:scaleDidChange:] + 62
frame #11: 0x0187207f UIKit`-[UIView _wrappedProcessDidChangeRecursivelyFromOldTraits:toCurrentTraits:scaleDidChange:forceNotification:] + 186
frame #12: 0x0187215e UIKit`-[UIView _wrappedProcessDidChangeRecursivelyFromOldTraits:toCurrentTraits:scaleDidChange:forceNotification:] + 409
frame #13: 0x0187232c UIKit`__86-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:]_block_invoke + 69
frame #14: 0x01f75040 UIKit`-[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 134
frame #15: 0x018722df UIKit`-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:] + 295
frame #16: 0x01880773 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 446
frame #17: 0x018805ad UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 458
frame #18: 0x0188c41d UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 2068
frame #19: 0x0187e976 UIKit`-[UIView(Hierarchy) addSubview:] + 56
frame #20: 0x0186d87e UIKit`-[UIView _setTraitStorageSubviews:] + 647
frame #21: 0x010ca608 Foundation`_NSSetUsingKeyValueSetter + 115
frame #22: 0x010ca58d Foundation`-[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
frame #23: 0x0188f13f UIKit`-[UIView(CALayerDelegate) setValue:forKey:] + 168
frame #24: 0x01100016 Foundation`-[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 386
frame #25: 0x01ab4d0e UIKit`-[_UIRelationshipTraitStorage applyRecordsMatchingTraitCollection:] + 1256
frame #26: 0x01ab3a89 UIKit`-[NSObject(_UITraitStorageAccessors) _applyTraitStorageRecordsForTraitCollection:] + 202
frame #27: 0x01871f84 UIKit`-[UIView _traitCollectionDidChangeFromOldCollection:toNewCollection:scaleDidChange:] + 62
frame #28: 0x0187207f UIKit`-[UIView _wrappedProcessDidChangeRecursivelyFromOldTraits:toCurrentTraits:scaleDidChange:forceNotification:] + 186
frame #29: 0x0187232c UIKit`__86-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:]_block_invoke + 69
frame #30: 0x01f75040 UIKit`-[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 134
frame #31: 0x018722df UIKit`-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:] + 295
frame #32: 0x0188f503 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 629
frame #33: 0x02c84771 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #34: 0x01614e47 QuartzCore`-[CALayer layoutSublayers] + 144
frame #35: 0x01608925 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 403
frame #36: 0x016172de QuartzCore`-[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 44
frame #37: 0x01880f08 UIKit`-[UIView(Hierarchy) layoutBelowIfNeeded] + 738
frame #38: 0x01880c11 UIKit`-[UIView(Hierarchy) layoutIfNeeded] + 83
frame #39: 0x01998bc5 UIKit`-[UINavigationController _layoutViewController:] + 1154
frame #40: 0x01997d41 UIKit`-[UINavigationController _layoutTopViewController] + 261
frame #41: 0x0199559a UIKit`-[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 584
frame #42: 0x01be54b4 UIKit`-[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 334
frame #43: 0x01be57c3 UIKit`-[UINavigationTransitionView _cleanupTransition] + 713
frame #44: 0x01be5810 UIKit`-[UINavigationTransitionView _navigationTransitionDidStop] + 55
frame #45: 0x0186a162 UIKit`-[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 206
frame #46: 0x0186803d UIKit`+[UIViewAnimationState popAnimationState] + 334
frame #47: 0x018834f4 UIKit`+[UIView(Animation) commitAnimations] + 36
frame #48: 0x01be52b3 UIKit`-[UINavigationTransitionView transition:fromView:toView:] + 2798
frame #49: 0x01be47bc UIKit`-[UINavigationTransitionView transition:toView:] + 56
frame #50: 0x019999b9 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 3123
frame #51: 0x01999d85 UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 669
frame #52: 0x0199aa01 UIKit`-[UINavigationController __viewWillLayoutSubviews] + 57
frame #53: 0x01b14700 UIKit`-[UILayoutContainerView layoutSubviews] + 213
frame #54: 0x0188f52a UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 668
frame #55: 0x02c84771 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #56: 0x01614e47 QuartzCore`-[CALayer layoutSublayers] + 144
frame #57: 0x01608925 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 403
frame #58: 0x0160877a QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
frame #59: 0x01564c22 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 284
frame #60: 0x015660b5 QuartzCore`CA::Transaction::commit() + 487
frame #61: 0x0162e84d QuartzCore`+[CATransaction flush] + 52
frame #62: 0x017f9a66 UIKit`-[UIApplication _reportMainSceneUpdateFinished:] + 39
frame #63: 0x017fa9d9 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 3182
frame #64: 0x018138ee UIKit`__84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
frame #65: 0x017f8ffa UIKit`-[UIApplication workspaceDidEndTransaction:] + 155
frame #66: 0x06ccfc9e FrontBoardServices`__37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
frame #67: 0x06ccf72f FrontBoardServices`__40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
frame #68: 0x06ce1d7c FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke_2 + 30
frame #69: 0x032fe050 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
frame #70: 0x032f3963 CoreFoundation`__CFRunLoopDoBlocks + 195
frame #71: 0x032f37bb CoreFoundation`__CFRunLoopRun + 2715
frame #72: 0x032f2a5b CoreFoundation`CFRunLoopRunSpecific + 443
frame #73: 0x032f288b CoreFoundation`CFRunLoopRunInMode + 123
frame #74: 0x017f89b2 UIKit`-[UIApplication _run] + 571
frame #75: 0x017fc0b6 UIKit`UIApplicationMain + 1526
frame #76: 0x00097bba Shop`main(argc=1, argv=0xbffb45f4) + 138 at main.m:14
frame #77: 0x03d7fac9 libdyld.dylib`start + 1
在iOS 9中似乎永远不会调用addSubview:
,但为什么呢?
答案 0 :(得分:3)
在故事板中禁用大小类会恢复所有缺少的子视图。
答案 1 :(得分:0)
I was having the same problem and figured out that it a lot of my elements and constraints weren't installed to the any/any size class. (This is because the scene was put together in Compact/Regular size class) For some reason when the app was initially loaded it was only loading the elements that were in any/any.
After I added all of elements and constraints to any/any everything was instantiated and added to the view.