Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 9 / iOS 11 "CALayer bounds contains NaN: [nan 0; nan 0]" when popping view controller with nested UINavigationController and UITabBarController

I am updating my application for Xcode 9, Swift 4, iOS 11 and the iPhone X. It seems to go relatively smooth all the way but whenever I hit the back button my application crashes. I can go forward 3-4 screens without any problem but the first back button crashes the application, always. It doesn't require the simulator to run as an iPhone X.

It doesn't seem to dip into my code in the stack trace so this is in my opinion the redraw phase of the view controller I'm popping to but I'm not sure.

Since I do quite a bit of custom drawing because there are custom shadows around UITableViews and UIViews I set up breakpoints in all locations where I divide by a variable, but nothing gets hit. So it doesn't seem to be my code that does the calculation by zero.

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [nan 0; nan 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010af711cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010a8d3f41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010afe5b95 +[NSException raise:format:] + 197
    3   QuartzCore                          0x0000000109424424 _ZN2CA5Layer10set_boundsERKNS_4RectEb + 230
    4   QuartzCore                          0x0000000109414c29 -[CALayer setBounds:] + 251
    5   UIKit                               0x0000000107267439 __27-[_UILabelLayer setBounds:]_block_invoke + 80
    6   UIKit                               0x000000010726717b -[_UILabelLayer _setFrameOrBounds:settingAction:] + 23
    7   UIKit                               0x00000001072673d8 -[_UILabelLayer setBounds:] + 155
    8   QuartzCore                          0x000000010941537c -[CALayer setFrame:] + 630
    9   UIKit                               0x0000000107267319 __26-[_UILabelLayer setFrame:]_block_invoke + 80
    10  UIKit                               0x000000010726717b -[_UILabelLayer _setFrameOrBounds:settingAction:] + 23
    11  UIKit                               0x00000001072672b8 -[_UILabelLayer setFrame:] + 155
    12  UIKit                               0x0000000106c4cf1e -[UIView(Geometry) setFrame:] + 368
    13  UIKit                               0x0000000106e4ec40 -[UILabel setFrame:] + 141
    14  UIKit                               0x0000000106fff254 -[UIButton _layoutTitleView] + 248
    15  UIKit                               0x0000000106fff3cf -[UIButton layoutSubviews] + 250
    16  UIKit                               0x0000000106c6c551 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1331
    17  QuartzCore                          0x000000010941b4ba -[CALayer layoutSublayers] + 153
    18  QuartzCore                          0x000000010941f5a9 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
    19  QuartzCore                          0x00000001093a81cd _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 365
    20  QuartzCore                          0x00000001093d3ae4 _ZN2CA11Transaction6commitEv + 500
    21  UIKit                               0x0000000106b97f4a _UIApplicationFlushRunLoopCATransactionIfTooLate + 167
    22  UIKit                               0x00000001074ef960 __handleEventQueueInternal + 6894
    23  CoreFoundation                      0x000000010af142b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    24  CoreFoundation                      0x000000010afb3d31 __CFRunLoopDoSource0 + 81
    25  CoreFoundation                      0x000000010aef8c19 __CFRunLoopDoSources0 + 185
    26  CoreFoundation                      0x000000010aef81ff __CFRunLoopRun + 1279
    27  CoreFoundation                      0x000000010aef7a89 CFRunLoopRunSpecific + 409
    28  GraphicsServices                    0x00000001104e59c6 GSEventRunModal + 62
    29  UIKit                               0x0000000106b9dd30 UIApplicationMain + 159
    30  My Customer's Application Name      0x000000010475f087 main + 55
    31  libdyld.dylib                       0x000000010cfedd81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
like image 824
Lucas van Dongen Avatar asked Sep 21 '17 14:09

Lucas van Dongen


3 Answers

TL;DR: In our case this iOS 11 / Xcode 9 error was caused by the code that added custom UILabel to the UINavigationItem.titleView.

This code was written using notorious swizzling technique that overrode -[UINavigationItem setTitle:] selector - along with calling the original setTitle: method our developer also set the custom label with the same title to the UINavigationItem.titleView property. The problem was the case when the navigation item was called with setTitle:nil so that empty UILabel was added to the .titleView with zero-rect dimensions and this caused UIKit to crash. The immediate quick fix was to stop setting the .titleView if the title: argument was nil. The long-term fix will be to remove swizzling from the app.

P.S. I have opened a radar with a request to improve the error message for this error:

Xcode 9: Please improve error handling: -[UILabel setFrame:] and -[CALayer setFrame:] throws unfriendly exception if given a malformed CGRect

like image 124
Stanislav Pankevich Avatar answered Oct 19 '22 13:10

Stanislav Pankevich


Since I did all the typing already when I fixed it (it always helps to phrase your question to somebody else ;-) ) I decided to post my question and the answer.

I have a navigation set up where a tab bar is nested in a navigation controller. I know we were warned against it, but hey, it worked like a charm all of the time. After setting large title views via an appearance proxy as follows:

    if #available(iOS 11.0, *) {
        UINavigationBar.appearance().prefersLargeTitles = true
    }

Screens started crashing when going back into them. Changing it back to false made all the problems disappear.

like image 29
Lucas van Dongen Avatar answered Oct 19 '22 15:10

Lucas van Dongen


I was getting this same issue, As the toolbar title was set to an empty string on the previous VC. Once I set all the titles with at least some kind of text, I stopped crashing. Again this was only on iOS 11 and Xcode 9, worked in previous versions.

like image 2
pjapple15 Avatar answered Oct 19 '22 13:10

pjapple15