0

我有一系列正在创建的按钮。但是,当我调用 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]'

4

1 回答 1

1

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

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

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

于 2013-04-13T23:25:54.703 回答