在for循环中创建的UIButtons的某些放置

时间:2014-03-20 04:56:50

标签: ios objective-c for-loop uiscrollview

我想创建UIButtons并将它们分隔在UIScrollView中,其页面如下:

enter image description here

按钮将从左到右然后按行向下,直到它到达视图的底部。然后它将转到UIScrollView的下一页并继续。

到目前为止,这是我的代码:

for(NSString *num in nums) {
        UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [test addTarget:self action:@selector(numPressed:) forControlEvents:UIControlEventTouchUpInside];
        test.frame = CGRectMake(0, 0, 70, 70);
        [self.view addSubview:test];
}

3 个答案:

答案 0 :(得分:1)

您需要在invokint CGRectMake

时更改坐标
NSInteger i, x, y;
for(NSString *num in nums) {
    ...
    i = i%3;
    if (i==0)
    {
        x = 0;
        y += 70;
    }
    x += 80;
    test.frame = CGRectMake(x, y, 70, 70);
    ...
}

这可以让您了解如何继续。另外,不要忘记将标题放在每个按钮上。

答案 1 :(得分:1)

有些代码如下:

int xOff = 10;
int yOff = 50;
int btnGap = 20;
int page = 0;
for (NSString *num in nums) {
       xOff += page * _scrollView.width;
        for (int row = 0; row < 3; row++) {
             for (int col = 0; col < 3; col++) {
                UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [test addTarget:self action:@selector(numPressed:) forControlEvents:UIControlEventTouchUpInside];
                test.frame = CGRectMake(xOff+col*(70+btnGap) , yOff+row*(70+btnGap), 70, 70);
                test.backgroundColor = [UIColor redColor];
                [test setTitle:num forState:UIControlStateNormal];
                 [_scrollView addSubview:test];
           }
        }
        page++;
    }

答案 2 :(得分:1)

如果您是逐帧创建和设置UIButton个对象,那么我个人会这样做(肯定可以改进/优化

注意:这不包含任何页面逻辑,因此您需要自己进行调整。

-(void)createButtonGrid
{
    int i_btnWidth = 70;
    int i_btnHeight = 70;
    int i_btnX = 0;
    int i_btnY = -70; //offset (for first iteration in for loop) [1]

    int i_btnPadding = 20;

    //number of columns
    int i_screenDivisions = 3;

    //width of every column
    int i_screenDivisionWidth = self.view.frame.size.width / i_screenDivisions;

    int i_count = 40; //or, maybe, nums.count as per your logic
    for (int i = 0; i < i_count; i++) {

        //which column a button goes in
        int i_index = (i+i_screenDivisions)%i_screenDivisions;

        //increase Y position when new row begins
        if (i_index == 0) { //[1]
            i_btnY += i_btnHeight + i_btnPadding;
        }

        //calculate X position (which will be placed at the centre of every column)
        i_btnX = (i_index*i_screenDivisionWidth) + (i_screenDivisionWidth/2) - (i_btnWidth/2);

        //----Button creation logic----
        UIButton *btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnTemp setFrame:CGRectMake(i_btnX, i_btnY, i_btnWidth, i_btnHeight)];

        NSString *strTitle = [NSString stringWithFormat:@"%d",i];
        //or, maybe, as per your logic
        //NSString *strTitle = nums[i];

        [btnTemp setTitle:strTitle forState:UIControlStateNormal];
        [btnTemp.titleLabel setTextColor:[UIColor yellowColor]];

        //[btnTemp addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
        [btnTemp setTag:i]; //might need it later
        [btnTemp setBackgroundColor:[UIColor colorWithRed:(float)(arc4random()%255)/255
                                                    green:(float)(arc4random()%255)/255
                                                     blue:(float)(arc4random()%255)/255
                                                    alpha:1.0f]];
        [scrollView addSubview:btnTemp];
    }

    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, i_btnY + i_btnHeight)];
}