当视图从 super 中删除时,ios12.0.1 上的应用程序崩溃

Application crash on ios12.0.1 when view remove from super

Date/Time: 2018-10-30 00:57:35 +0000 OS Version: iPhone OS 12.0.1 (16A404) Report Version: 104

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x10
Crashed Thread:  0

Thread 0 Crashed: 0 libobjc.A.dylib
0x00000001aa9f0d70 objc_msgSend + 16 1 Foundation
0x00000001ac3c7958 NSLayoutConstraintIsPotentiallyDanglyInContainer + 112 2 UIKitCore 0x00000001d8b78464 -[UIView+ 13431908 (AdditionalLayoutSupport) _snipDangliesWithForce:repairIfPossibleForViewThatMoved:newSuperview:oldSuperview:] + 424 3 UIKitCore 0x00000001d8b781c8 _UIViewRemoveConstraintsMadeDanglyByChangingSuperview + 1088 4 UIKitCore 0x00000001d8c19f54 __45-[UIView+ 14094164 (Hierarchy) _postMovedFromSuperview:]_block_invoke + 68 5
UIKitCore 0x00000001d8c19e74 -[UIView+ 14093940 (Hierarchy) _postMovedFromSuperview:] + 756 6 UIKitCore
0x00000001d8c17f38 __UIViewWasRemovedFromSuperview + 172 7 UIKitCore 0x00000001d8c17a18 -[UIView+ 14084632 (Hierarchy) removeFromSuperview] + 464

我收到过这样的崩溃,只有ios12.0.1

我的声誉不够高,无法发表评论,但我遇到了同样的崩溃,直到 iOS 12 才开始发生。我向 Apple 提交了一份错误报告,希望它能尽快得到解决。

根据堆栈跟踪,它在尝试清理悬挂约束时崩溃,因此您可以尝试的一种可能的解决方法是在移除视图之前移除所有约束。

extension UIView {
    func removeAllConstraints() {
        let superViewConstraints = superview?.constraints.filter{ [=10=].firstItem === self || [=10=].secondItem === self } ?? []

        superview?.removeConstraints(superViewConstraints + constraints)
    }
}

...然后在删除视图之前调用:

view.removeAllConstraints() 

我不是 100% 确定这会起作用,因为我自己无法重现崩溃,我只看到它在我使用的崩溃分析服务上弹出。