views:

135

answers:

1

hello i am stuck in a strange situation.i want to generate buttons on a view using a loop . suppose i want to generate 3 buttons.i have added a uiview btnframe to my main view.now i want to add button inside that view using that view's cordinates.how do i calculate btnframe's bounds.

+3  A: 

Well what size do you want your buttons to be? If you add them to a view they inherit those cordinates so if you do 0,0 it will be in the top left corner of the view you add it to.

for(int x=0;x<3;x++){
CGRect rect = CGRectMake(0,20 * x,100,20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[btnframe addSubview:button];
.....
}

The basics, this would give you three buttons.

If you want a grid something like this would work.

for(int x=0;x<5;x++){
    for(int y=0;y<5;y++){
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x * 100, y * 20, 100, 20)];
     [button setText:[NSString stringWithFormat:@"%d,%d",x,y]];
     [button addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
     [mainView addSubview:button];
    }
}

This gives you 25 buttons, 5 on each row.

Ryan Detzel
i want to generate something like a grid view with each button leads to a new view
Rahul Vyas