* Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 40]'

今天早上上班,发现程序就直接挂了,昨天调得好好的,怎么来上班就挂了呢?

栈信息打印如下:

2015-10-20 10:01:12.871 Putaoji[49129:1691777] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 40]' *** First throw call stack: (    0   CoreFoundation                      0x000000010df49f65 __exceptionPreprocess + 165    1   libobjc.A.dylib                     0x000000010d244deb objc_exception_throw + 48    2   CoreFoundation                      0x000000010df49e9d +[NSException raise:format:] + 205    3   QuartzCore                          0x000000010cc2f7e6 _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 152    4   QuartzCore                          0x000000010cc2f959 -[CALayer setPosition:] + 44    5   QuartzCore                          0x000000010cc2ffbd -[CALayer setFrame:] + 650    6   UIKit                               0x000000010e9673f7 -[UIView(Geometry) setFrame:] + 356    7   UIKit                               0x000000010ec50422 -[UIButton _setFrame:deferLayout:] + 125    8   UIKit                               0x000000010ec50522 -[UIButton setFrame:] + 178    9   Putaoji                             0x000000010ae41132 -[PTJInviteFriendsViewController createShareButtonWithFrame:atIndex:title:p_w_picpathName:] + 690    10  Putaoji                             0x000000010ae4004b -[PTJInviteFriendsViewController setupUI] + 2715    11  Putaoji                             0x000000010ae3f5a4 -[PTJInviteFriendsViewController viewDidLoad] + 228    12  UIKit                               0x000000010ea59931 -[UIViewController loadViewIfRequired] + 1344    13  UIKit                               0x000000010ea5f923 -[UIViewController __viewWillAppear:] + 120    14  UIKit                               0x000000010ea8f18a -[UINavigationController _startCustomTransition:] + 1177    15  UIKit                               0x000000010ea9e7c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 712    16  UIKit                               0x000000010ea9f67d -[UINavigationController __viewWillLayoutSubviews] + 57    17  UIKit                               0x000000010ec3763d -[UILayoutContainerView layoutSubviews] + 248    18  UIKit                               0x000000010e97f11c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710    19  QuartzCore                          0x000000010cc3836a -[CALayer layoutSublayers] + 146    20  QuartzCore                          0x000000010cc2cbd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366    21  QuartzCore                          0x000000010cc2ca4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24    22  QuartzCore                          0x000000010cc211d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277    23  QuartzCore                          0x000000010cc4e9f0 _ZN2CA11Transaction6commitEv + 508    24  UIKit                               0x000000010e8f853a _afterCACommitHandler + 174    25  CoreFoundation                      0x000000010de759d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23    26  CoreFoundation                      0x000000010de75947 __CFRunLoopDoObservers + 391    27  CoreFoundation                      0x000000010de6b59b __CFRunLoopRun + 1147    28  CoreFoundation                      0x000000010de6ae98 CFRunLoopRunSpecific + 488    29  GraphicsServices                    0x0000000112b04ad2 GSEventRunModal + 161    30  UIKit                               0x000000010e8ce676 UIApplicationMain + 171    31  Putaoji                             0x000000010af306ff main + 111    32  libdyld.dylib                       0x000000010fe9d92d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

解决方法:

实际标题已经很清楚的写到:>* Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 40]'

意思是说程序崩溃是CALayer的位置中含有不存在的数,说白了就是你的View.frame 中计算的时候,有的地方除以0了。例如在我的程序中计算的时候:

 CGFloat leftSpace = 24.0f;    CGFloat width = SCREENWIDTH;    CGFloat itemWH = 80.0-22.0;    NSInteger count = shareTitleArray.count;    CGFloat innerSpace = (width -leftSpace*2 - itemWH *count)/(shareTitleArray.count -1);    for (int i = 0; i<shareTitleArray.count; i++)
    {        CGFloat x = leftSpace+i*(innerSpace+itemWH);        CGRect frame = CGRectMake(x, 11, itemWH, itemWH);
        [self createShareButtonWithFrame:frame atIndex:i title:shareTitleArray[i] p_w_picpathName:shareImageArray[i]];
    }

仔细一看没有什么问题,但是如果shareTitleArray数组中的个数为1的情况的下,就会导致除以0,从而导致程序崩溃。