1

I have a slight problem with an app I would like some help with. I've searched the web and found some similar problems but they're not entirely answering my own problem.

I want to create 42 buttons and print them out in a "calendar way". It's not suppose to be a calendar but the appearance reminds of it. I tried out this Create a for loop to add 39 buttons to an array

But I could't figure out how to make it do exactly what I want.

I've also experimented with this:

NSMutableArray *buttons = [NSMutableArray array];

for( int i = 0; i < 5; i++ ) {

    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.backgroundColor = [UIColor redColor];
    aButton.frame = CGRectMake(10, (i+1)*60, 60, 40);
    [aButton setTag:i];
    [buttons addObject:aButton];
    [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:aButton];
}

And it's on the right way but I just can't figure out how to get the look I want. Here the look I'm after, but the numbeers will range from 1 to 42: http://nebulon.se/images/question_img.png

Community
  • 1
  • 1

1 Answers1

0

To layout a grid you want a loop in a loop. The outer loop incrementing the y coordinate and the inner loop incrementing the x coordinate. So, as you progress you work across each column in a row of buttons and then move onto the next row. Something like:

for( int i = 0; i < 5; i++ ) {
    for( int j = 0; j < 5; j++ ) {

        UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(j * 60, i * 60, 60, 40);

    }
}
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Exactly! How do i get some spacing between the buttons? They line up exactly next to each other. I'm guessing it is something like this: aButton.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40); –  Aug 23 '13 at 12:05
  • Yeah, tweaking the `x` and `y` will give you spacing. – Wain Aug 23 '13 at 12:30
  • Thanks for the fast reply! –  Aug 23 '13 at 12:32