1

I've added some buttons to an UIView (via addSubview) programmatically. However, they appear as overlays (so that I always see the last button only). How do I add new buttons below existing buttons?

Regards

2
  • Could you clarify the question by stating which axis you are referring to when you say 'below'?
    – teabot
    Jun 29, 2009 at 15:34
  • Yes, I meant the y-axis.
    – Stefan
    Jun 29, 2009 at 16:14

4 Answers 4

4

you can offset the button like this

int newX = previousButton.frame.origin.x + previousButton.frame.size.width ;
int newY = previousButton.frame.origin.y ;

and either set the frame for new button when you create it:

[[UIButton alloc] initWithFrame:CGRectMake(newX,newY,100,100)];

or set the frame later

newButton.frame = CGRectMake(newX,newY,100,100);
1
  • What a coincidence, I made a similar solution at the same time ;-) Here is mine: roundedButton.frame = CGRectMake(previousFrame.origin.x + previousFrame.size.width + 5, 0, 200,30); Thanks!
    – Stefan
    Jun 29, 2009 at 16:10
3

Set the UIView's frame origin to layout the UIButtons in the locations you wish:

CGRect buttonFrame = button.frame;
buttonFrame.origin = CGPointMake(100.0f, 100.0f);
button.frame = buttonFrame;
view.addSubview(button);
1
  • 1
    You'll need to add "button.frame = buttonFrame;" after line 2 or 3 to make this work. Otherwise, you're just modifying a copy of the frame rectangle.
    – e.James
    Jun 29, 2009 at 15:00
2

You can either use the insertSubview:atIndex method or insertSubview:belowSubview of your view.

UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)];

[myView insertSubview:myButton belowSubview:previousButton];

OR

[myView insertSubview:myButton atIndex:0];
2
  • I was uncertain whether Stefan was referring to the Y or Z axis - but between us we have covered both.
    – teabot
    Jun 29, 2009 at 15:13
  • Couldn't really figure that out either, but like you said we took care of both.
    – Ron Srebro
    Jun 29, 2009 at 15:35
0

Thanks for your answers guys.

I did the (horizontal) align with this code:

if([myContainer.subviews lastObject] == nil){
        NSLog(@"NIL");
        [myContainer insertSubview:roundedButton atIndex:0];
    }else{
        [myContainer insertSubview:roundedButton belowSubview:[tagsContainer.subviews lastObject]];
    }

It works technically, but still overlays the buttons. I have to find a way, how to not overlay them...

2
  • What do you mean overlay the buttons?
    – Ron Srebro
    Jun 29, 2009 at 15:38
  • They appear visually as one button, because the are still on the same position (0.0, 0.0). So, my aim is to get the new starting position from the previous UIButton ([myContainer.subviews lastObject]). I think it has something to do with the UIButtons frame. But I don't know, how to get the positions.
    – Stefan
    Jun 29, 2009 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.