Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failure in [MyClass layoutSublayersOfLayer:]

I'm using AutoLayout in a subclass of UITextField, but sometimes I'm getting the following error/stacktrace:

*** 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

What am I missing?

like image 911
swalkner Avatar asked Jul 14 '14 07:07

swalkner


2 Answers

I had a similar issue with a custom view and it turns out that one of my custom parent classes was overriding -layoutSubviews and calling super, only it was calling super first instead of last. What fixed it for me was making sure all overrides to -layoutSubviews in my project were of form:

- (void)layoutSubviews {
  // Custom code which potentially messes with constraints
  [super layoutSubviews]; // No code after this and this is called last
}
like image 89
Nicholas H. Avatar answered Nov 02 '22 18:11

Nicholas H.


I just started experiencing this problem out of the blue, on two VCs that were working fine.

Turns out, adding

[self.view layoutIfNeeded];

as the last line of the viewDidLayoutSubviews method on my VC fixed it for me.

I have no idea why this worked or why it broke to begin with, but most of our testing is against iOS 8 and as we were working through issues running under 7 this cropped up.

Couldn't find a solution that worked for this particular problem, so hopefully this will help someone else out!

like image 17
Raconteur Avatar answered Nov 02 '22 19:11

Raconteur