Xcode 9 / iOS 11 "CALayer bounds contains NaN: [nan 0; nan 0]" when a controller appears with an embedded UINavigationController and UITabBarController

I am updating my application for Xcode 9, Swift 4, iOS 11 and iPhone X. It seems to be going pretty smoothly, but whenever I press the back button, my application crashes. I can go to 3-4 screens without any problems, but the first return button always resets the application. This does not require the simulator to work like an iPhone X.

It seems that it does not plunge into my code in the stack trace, so this, in my opinion, is the phase of redrawing the view controller, which I come to, but I'm not sure.

Since I am doing quite a lot of custom drawing because there are custom shadows around UITableViews and UIViews, I set breakpoints in all the places where I am dividing by a variable but nothing gets caught. So this is not like my code that evaluates to 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 Application Name 0x000000010475f087 main + 55 31 libdyld.dylib 0x000000010cfedd81 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException 
+7
swift ios11 xcode9
source share
4 answers

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

This code was written using the infamous swizzling method, which redo the selector -[UINavigationItem setTitle:] - along with calling the original setTitle: method setTitle: our developer also sets a custom label with the same name in the UINavigationItem.titleView property. The problem was that the navigation element was called using setTitle:nil , so that an empty UILabel was added to the .titleView with null values, and this caused UIKit to crash. The immediate quick .titleView was to stop installing .titleView if the title: argument was zero. The long-term solution is to remove swizzling from the application.

PS I opened the radar with a request to improve the error message for this error:

Xcode 9: improve error handling: - [UILabel setFrame:] and - [CALayer setFrame:] throws an unfriendly exception if an invalid CGRect is specified

+9
source share

Since I already dialed everything when I corrected it (it always helps to tell my question to someone else ;-)) I decided to publish my question and answer.

I have a navigation setting where the tab bar is nested in the navigation controller. I know we were warned about this, but hey, it worked like a charm all the time. After setting large headers through the appearance proxy:

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

Screens started crashing when returning to them. Changing it to false led to the disappearance of all problems.

+3
source share

I was getting the same problem since the toolbar title was set to an empty line in the previous VC. As soon as I set all the headers, at least with some text, I stopped crashing. Again, this was only on iOS 11 and Xcode 9, which worked in previous versions.

+2
source share

This happened to me when I installed the specified ViewController with the NavigationBar, preferring large titles = true.

I solved it the same way as pjapple15 . I just set the title as an empty string = "" in the NavigationBar of the previous ViewController and worked fine.

And I also added SearchBar to NavigationBar.titleView, after that I just set NavigationItem.title = "".

+1
source share

All Articles