UIButton setCenter崩溃

时间:2013-04-13 23:18:59

标签: objective-c xcode

我正在创建一系列按钮。但是,当我调用setCenter时,代码崩溃了。

buttonsArray = [NSMutableArray new];
    for (int i = 0; i < 7; i++) {
        [buttonsArray addObject:[UIButton buttonWithType:UIButtonTypeCustom]];
        UIButton *tempButton = [buttonsArray objectAtIndex:i];
        [tempButton setFrameWidth:300.0 andHeight:50.0];
        [tempButton addBlackBorderWidth:1.0];
        [tempButton roundCornersBy:10.0];
        [tempButton setBackgroundColor:[UIColor blackColor]];
        [tempButton setColorToGradientFromColor:[UIColor grayColor] toColor:[UIColor blackColor]];
        [tempButton setButtonTextColorForNormalState:[UIColor whiteColor] highlightedState:[UIColor redColor]];
        [tempButton setCenter:CGPointMake([self screenUsableWidth] / 2.0, ([self screenUsableHeight] - 2.0 * MAIN_MENU_BUTTON_TO_SIDE_DISTANCE) * i / ([buttonsArray count] - 1) + MAIN_MENU_BUTTON_TO_SIDE_DISTANCE)]; //crash is here
    }

错误为*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [384 nan]'

1 个答案:

答案 0 :(得分:1)

y坐标计算的一部分除以([buttonsArray count] - 1)

第一次在循环中,这相当于0.除以0通常被认为是一件坏事。

您需要更改该计算以避免除以零。也许将其更改为实际的循环计数而不是当前的数组计数。