简体   繁体   English

iOS8中的自动布局崩溃

[英]Autolayout crash in iOS8

Programmatically adding subview to UIScrollView from xib with autolayout turned on. xib autolayout打开的情况下,以编程方式从xib将子视图添加到UIScrollView Receiving crash: 接收崩溃:

2015-02-25 22:21:25.829 EAIntroView[5215:1435025] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c1f4f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010be8dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010c0dff33 -[__NSArrayM objectAtIndex:] + 227
    3   UIKit                               0x000000010c87b5b1 _UIViewTopDownSubtreeTraversal + 193
    4   UIKit                               0x000000010ceacee6 -[UIView(UIConstraintBasedLayout_EngineDelegate) _invalidateSystemLayoutSizeFittingSizeAtEngineDelegateLevel] + 128
    5   Foundation                          0x000000010ba319bf -[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:] + 915
    6   Foundation                          0x000000010bbbb0f8 -[NSLayoutConstraint _addLoweredExpression:toEngine:integralizationAdjustment:lastLoweredConstantWasRounded:mutuallyExclusiveConstraints:] + 275
    7   Foundation                          0x000000010ba2611a -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 220
    8   UIKit                               0x000000010cea9de5 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
    9   Foundation                          0x000000010ba33d6e -[NSISEngine withBehaviors:performModifications:] + 155
    10  UIKit                               0x000000010cea9beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    11  UIKit                               0x000000010cea99fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    12  UIKit                               0x000000010cea9ce4 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
    13  Foundation                          0x000000010ba33d6e -[NSISEngine withBehaviors:performModifications:] + 155
    14  UIKit                               0x000000010cea9beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    15  UIKit                               0x000000010cea99fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    16  UIKit                               0x000000010cea9ce4 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
    17  Foundation                          0x000000010ba33d6e -[NSISEngine withBehaviors:performModifications:] + 155
    18  UIKit                               0x000000010cea9beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    19  UIKit                               0x000000010cea99fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    20  UIKit                               0x000000010c8a568b -[UIScrollView _switchToLayoutEngine:] + 69
    21  UIKit                               0x000000010cea9ce4 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
    22  Foundation                          0x000000010ba33d6e -[NSISEngine withBehaviors:performModifications:] + 155
    23  UIKit                               0x000000010cea9beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    24  UIKit                               0x000000010cea99fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    25  UIKit                               0x000000010cea9ce4 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
    26  Foundation                          0x000000010ba33d6e -[NSISEngine withBehaviors:performModifications:] + 155
    27  UIKit                               0x000000010cea9beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    28  UIKit                               0x000000010cea99fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    29  UIKit                               0x000000010cea9671 -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
    30  UIKit                               0x000000010ce9e591 -[UIView(UIConstraintBasedLayout) _layoutEngine_windowDidChange] + 127
    31  UIKit                               0x000000010c8984a5 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 207
    32  UIKit                               0x000000010c891112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
    33  UIKit                               0x000000010c891086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
    34  UIKit                               0x000000010c89af4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604
    35  UIKit                               0x000000010cabfe84 -[UILayoutContainerView addSubview:] + 75
    36  EAIntroView                         0x000000010b93e14a -[EAIntroView showInView:animateDuration:withInitialPageIndex:] + 362
    37  EAIntroView                         0x000000010b93dfc9 -[EAIntroView showInView:animateDuration:] + 121
    38  EAIntroView                         0x000000010b9303a7 -[ViewController showIntroWithCustomViewFromNib] + 1879
    39  EAIntroView                         0x000000010b9326a4 -[ViewController tableView:didSelectRowAtIndexPath:] + 340
    40  UIKit                               0x000000010c90c393 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
    41  UIKit                               0x000000010c90c4d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    42  UIKit                               0x000000010c847331 _applyBlockToCFArrayCopiedToStack + 314
    43  UIKit                               0x000000010c8471ab _afterCACommitHandler + 516
    44  CoreFoundation                      0x000000010c129dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    45  CoreFoundation                      0x000000010c129d20 __CFRunLoopDoObservers + 368
    46  CoreFoundation                      0x000000010c11fb53 __CFRunLoopRun + 1123
    47  CoreFoundation                      0x000000010c11f486 CFRunLoopRunSpecific + 470
    48  GraphicsServices                    0x00000001106f59f0 GSEventRunModal + 161
    49  UIKit                               0x000000010c824420 UIApplicationMain + 1282
    50  EAIntroView                         0x000000010b932843 main + 115
    51  libdyld.dylib                       0x000000010e9c7145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Doesn't matter if there are any autolayout constraints in xib . 如果有任何自动布局约束不要紧xib Adding autolayout constraints to programmatically created subviews (without xib ) - leads to the same crash. 将自动布局约束添加到以编程方式创建的子视图(不带xib )会导致相同的崩溃。

But: if we create and show custom view without any autolayout, hide it and create new with autolayout xib - it will work perfectly, even with all constraints working. 但是:如果我们创建并显示没有任何自动布局的自定义视图,请将其隐藏并使用自动布局xib创建新xib -即使在所有约束都起作用的情况下,它也将完美运行。

Related SO question: NSRangeException on iOS 8 相关的问题: iOS 8上的NSRangeException

Solution from that question: 该问题的解决方案:

Turning AutoLayout off for all of my XIBs solved the issue. 为我的所有XIB关闭AutoLayout可解决此问题。

All code, example project and related issue on GitHub: https://github.com/ealeksandrov/EAIntroView/issues/100 GitHub上的所有代码,示例项目和相关问题: https//github.com/ealeksandrov/EAIntroView/issues/100

I finally found a root cause of this issue: subviews property override in my UIScrollView subclass ( EARestrictedScrollView.m#L54-56 ). 我终于找到了这个问题的根本原因: subviews属性重写我UIScrollView子类( EARestrictedScrollView.m#L54-56 )。

Autolayout somehow uses subview property and overriding it breaks things. 自动布局以某种方式使用subview属性,并且覆盖它会破坏事情。 This is inconsistent and depends on many other things, but fixing subviews deals it for me in 100% of cases. 这是不一致的,并且取决于许多其他因素,但是在100%的情况下,修复subviews对我来说都是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM