D34thSt4lker
D34thSt4lker

Reputation: 447

How can I create an array of UIButtons/(UIImageViews) that appear after the last one was pressed

I wasn't sure how to search for the answer to this question so I figured I would try my luck over here.

To put this in short, I have a UIView and created a UIButton programmatically along with a UIImageView added to the button as a subview and what happens is, when I press on the button, it allows me to add an image and it gets stored over the button.

So my question is: Once that is completed, how can I make it that another button/ImageView will appear in the view, next to the first button (or after it fills the screen to the right, appear in the 2nd row, 1st column) etc

Thank you in advanced!

Upvotes: 0

Views: 75

Answers (3)

iphondroid
iphondroid

Reputation: 500

You can use UICollectionView as pointed out by @Bernahard Harrer or you could use the following code. This code adds only two buttons. In case, you need more buttons or an array of buttons, implement the following code and calculate the size of the previously added button, as height/width of button1 is calculated for adding button2:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    [self addButtons];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) addButtons
{
    UIView *view = [self view];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setBackgroundImage:[UIImage imageNamed:@"buttonImage1"] forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setBackgroundImage:[UIImage imageNamed:@"buttonImage2"] forState:UIControlStateNormal];

[button1 sizeToFit];
[button2 sizeToFit];


CGFloat combinedWidth = (button1.frame.size.width) + (button2.frame.size.width) ;

//checking if combined width of both buttons is more than 320
if (combinedWidth>320) {
    CGFloat button1height = button1.frame.size.height;
    button1height = button1height + 10;
    [button2 setFrame:CGRectMake(button2.frame.origin.x, (button2.frame.origin.y + button1height), button2.frame.size.width, button2.frame.size.width)];
} else {
    //if combined width of both buttons is less than or equal to 320
    //this will place the buttons adjacent to each other.
    //The code may be modified accordingly if to add some distance between them
    CGFloat button1width = button1.frame.size.width;
    [button2 setFrame:CGRectMake((button1.frame.origin.x+button1width), button2.frame.origin.y, button2.frame.size.width, button2.frame.size.height)];
}

[view addSubview:button1];
[view addSubview:button2];
}@end

Upvotes: 1

aahsanali
aahsanali

Reputation: 3547

@interface ViewController (){

    NSInteger x;
    NSInteger y;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    x = 4;y=100;


    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)addViews:(id)sender {

    CGRect frame = CGRectMake(x, y, 40, 10);

    int offset = 30;

    x = (frame.origin.x + frame.size.width + offset);

    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:[UIColor redColor]];

    if (view.center.x+view.frame.size.width > 320) {
        y +=20;
        x = 4;
    }
    [view setFrame:frame];

    [self.view addSubview:view];



}

@end

Upvotes: 0

Bernhard Harrer
Bernhard Harrer

Reputation: 125

You can try to use a UICollectionView for this issue.

Upvotes: 0

Related Questions