[MyClass layoutSublayersOfLayer:] 中的断言失败

Posted

技术标签:

【中文标题】[MyClass layoutSublayersOfLayer:] 中的断言失败【英文标题】:Assertion failure in [MyClass layoutSublayersOfLayer:] 【发布时间】:2014-07-14 07:29:27 【问题描述】:

我在 UITextField 的子类中使用 AutoLayout,但有时我会收到以下错误/堆栈跟踪:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. KOAOneDigitTextField's implementation of -layoutSubviews needs to call super.'

*** First throw call stack:
(
    0   CoreFoundation                      0x0390d1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x034c08e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0390d048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0167e4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x02615a38 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 567
    5   libobjc.A.dylib                     0x034d282b -[NSObject performSelector:withObject:] + 70
    6   QuartzCore                          0x01caf45a -[CALayer layoutSublayers] + 148
    7   QuartzCore                          0x01ca3244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    8   QuartzCore                          0x01ca30b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    9   QuartzCore                          0x01c097fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    10  QuartzCore                          0x01c0ab85 _ZN2CA11Transaction6commitEv + 393
    11  QuartzCore                          0x01cc85b0 +[CATransaction flush] + 52
    12  UIKit                               0x025a49bb _UIApplicationHandleEventQueue + 13095
    13  CoreFoundation                      0x0389677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    14  CoreFoundation                      0x0389610b __CFRunLoopDoSources0 + 235
    15  CoreFoundation                      0x038b31ae __CFRunLoopRun + 910
    16  CoreFoundation                      0x038b29d3 CFRunLoopRunSpecific + 467
    17  CoreFoundation                      0x038b27eb CFRunLoopRunInMode + 123
    18  GraphicsServices                    0x0530c5ee GSEventRunModal + 192
    19  GraphicsServices                    0x0530c42b GSEventRun + 104
    20  UIKit                               0x025a6f9b UIApplicationMain + 1225
    21  MyProject                          0x0006e94d main + 141
    22  libdyld.dylib                       0x03e86701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我错过了什么?

【问题讨论】:

***.com/questions/12610783/… ***.com/questions/19837097/… ***.com/questions/13875605/… 他们在谈论 UITableViewCell - 解决方案是将内容放入 contentView - 但我这里不处理 UITableViewCells。 您能否详细说明您要在子类中完成的工作?从课堂上发布一些相关代码也会有所帮助 【参考方案1】:

我在自定义视图中遇到了类似的问题,结果发现我的一个自定义父类覆盖了-layoutSubviews 并调用了super,只是它首先调用super 而不是last时间>。为我解决的问题是确保我的项目中对-layoutSubviews 的所有覆盖都具有以下形式:

- (void)layoutSubviews 
  // Custom code which potentially messes with constraints
  [super layoutSubviews]; // No code after this and this is called last

【讨论】:

【参考方案2】:

我刚开始在两个运行良好的 VC 上突然遇到这个问题。

原来,添加

[self.view layoutIfNeeded];

作为我的 VC 上 viewDidLayoutSubviews 方法的最后一行,为我修复了它。

我不知道为什么这会奏效,也不知道为什么一开始就崩溃了,但我们的大部分测试都是针对 ios 8 的,并且在我们解决运行在 7 下的问题时,这突然出现了。

找不到解决此特定问题的解决方案,因此希望这对其他人有帮助!

【讨论】:

我遇到了同样的问题,我的应用在 iOS 8 上运行良好,但在 iOS 7 上却报错。 与我的应用程序相同。这有帮助。谢谢

以上是关于[MyClass layoutSublayersOfLayer:] 中的断言失败的主要内容,如果未能解决你的问题,请参考以下文章

Myclass.class与new MyClass()有什么区别?

#include "MyClass.h" 和 #include ".\myclass.h" 有啥区别

c ++不存在从“myClass1”到“myClass1”的合适的用户定义转换

为啥 `throw MyClass' 不起作用而 `throw MyClass()' 起作用?

“MyClass& func1(void)”和“MyClass* func2(void)”之间的区别 [重复]

如何使用 ArrayAdapter<myClass>