Jakub
Jakub

Reputation: 13860

adding buttons below the other

I want to add buttons one below the other. I have this simple code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for(int i=0 ; i<9 ; i++)
    {
        UIButton *myButton = [[UIButton alloc] init];
        myButton.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height/10); //this "10" i want also dynamically
        myButton.backgroundColor = [UIColor blackColor];
        [self.view addSubview:myButton];
    }

}

Of course i know that it will be draw one on another. But can i do it in the loop without knowing the height (becouse high will depends on how many buttons will be in the loop).

What i want to achieve:

Buttons

Upvotes: 0

Views: 121

Answers (3)

Diremage
Diremage

Reputation: 200

Perhaps this might work? Sorry I haven't used self.view.frame in ages, but you get the general idea.

- (void)viewDidLoad
{
    [super viewDidLoad];
    int number;

    for(int i=0 ; i<9 ; i++)
    {
        UIButton *myButton = [[UIButton alloc] init];
        myButton.frame = CGRectMake(i * self.view.frame.origin.x / number,
                                    self.view.frame.origin.y,
                                    self.view.frame.size.width,
                                    self.view.frame.size.height / number); //this "10" i want also dynamically
        myButton.backgroundColor = [UIColor blackColor];
        [self.view addSubview:myButton];
    }
}

Upvotes: 0

holex
holex

Reputation: 24041

you need to define the size of the buttons for the correct show, unfortunately without the size of the buttons you cannot achieve you wished because how else does the system know what sized button you'd like to show...?

- (void)viewDidLoad
{
    [super viewDidLoad];

    Float32 _spaceBetweenButtons = 8.f;
    Float32 _offsetY = 32.f;
    Float32 _buttonWidth = 300.f; // width fo button
    Float32 _buttonHeight = 32.f; // height of the button

    for (int i = 0 ; i < 9 ; i++) {
        UIButton *_myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, _buttonWidth, _buttonHeight)];
        [_myButton setBackgroundColor:[UIColor blackColor]];
        [_myButton setTitle:[NSString stringWithFormat:@"button #%d", i] forState:UIControlStateNormal]; // just add some text as title
        [self.view addSubview:_myButton];
        [_mybutton setCenter:CGPointMake(self.view.frame.size.width / 2.f, _offsetY + i * (myButton.frame.size.height + _spaceBetweenButtons))];
    }
}

if you want to calculate the size of the buttons dynamically and you want to align the button's height to the view's height, here would be one way:

NSInteger _numberOfButtons = 20;
Float32 _spaceBetweenButtons = 8.f;
Float32 _calculatedHeight = (self.view.frame.size.height - (_numberOfButtons + 1 * _spaceBetweenButtons)) / _numberOfButtons;

and the method is same as above, but I'm not sure you will get good UI in case of hundreds buttons. :)

Upvotes: 0

No Name
No Name

Reputation: 1543

Try This:

- (void)viewDidLoad
  {
    [super viewDidLoad];
    int number = 10;

     for(int i=0 ; i<9 ; i++)
    {
    UIButton *myButton = [[UIButton alloc] init];
    myButton.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.size.height/number)*i + number, self.view.frame.size.width, self.view.frame.size.height/number);
    myButton.backgroundColor = [UIColor blackColor];
    [self.view addSubview:myButton];
    }


}

Upvotes: 1

Related Questions