How to define a margin for these UIButtons

advertisements

I created a view with 3 buttons in Xcode, the codes are as follows:

UIImage *firstButtonImg = [UIImage imageNamed:(@"firstButton")];
UIImage *firstButtonImgHighlighted = [UIImage imageNamed:(@"firstButtonHightlighted")];
self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setBackgroundImage:firstButtonImg forState:UIControlStateNormal];
[firstButton setBackgroundImage:firstButtonImgHighlighted forState:UIControlStateHighlighted];
self.firstButton.frame = CGRectMake(50, 60+self.aboutViewTop.frame.size.height+20, firstButtonImg.size.width, firstButtonImg.size.height);
[firstButton addTarget:self action:@selector(firstButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];

UIImage *secondButtonImg = [UIImage imageNamed:(@"secondButton")];
UIImage *secondButtonImgHighlighted = [UIImage imageNamed:(@"secondButtonHighlighted")];
self.secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[secondButton setBackgroundImage:secondButtonImg forState:UIControlStateNormal];
[secondButton setBackgroundImage:secondButtonImgHighlighted forState:UIControlStateHighlighted];
self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height);
[secondButton addTarget:self action:@selector(secondButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];

UIImage *thirdButtonImg = [UIImage imageNamed:(@"thirdButton")];
UIImage *thirdButtonImgHighlighted = [UIImage imageNamed:(@"thirdButtonHighlighted")];
self.thirdButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thirdButton setBackgroundImage:thirdButtonImg forState:UIControlStateNormal];
[thirdButton setBackgroundImage:thirdButtonImgHighlighted forState:UIControlStateHighlighted];
self.thirdButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+secondButton.frame.size.height+20.f, thirdButtonImg.size.width, thirdButtonImg.size.height);
[thirdButton addTarget:self action:@selector(thirdButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdButton];

but these buttons are closely placed, without the 20 space I added between them, I can't figure out why

Please help me


sorry guys

problem solved

it seems that I can't add numbers to rect multiple times

the right codes:

UIImage *firstButtonImg = [UIImage imageNamed:(@"firstButton")];
UIImage *firstButtonImgHighlighted = [UIImage imageNamed:(@"firstButtonHightlighted")];
self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setBackgroundImage:firstButtonImg forState:UIControlStateNormal];
[firstButton setBackgroundImage:firstButtonImgHighlighted forState:UIControlStateHighlighted];
self.firstButton.frame = CGRectMake(50, 80+self.aboutViewTop.frame.size.height, firstButtonImg.size.width, firstButtonImg.size.height);
[firstButton addTarget:self action:@selector(firstButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstButton];

UIImage *secondButtonImg = [UIImage imageNamed:(@"secondButton")];
UIImage *secondButtonImgHighlighted = [UIImage imageNamed:(@"secondButtonHighlighted")];
self.secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[secondButton setBackgroundImage:secondButtonImg forState:UIControlStateNormal];
[secondButton setBackgroundImage:secondButtonImgHighlighted forState:UIControlStateHighlighted];
self.secondButton.frame = CGRectMake(50, 100+aboutViewTop.frame.size.height+firstButton.frame.size.height, secondButtonImg.size.width, secondButtonImg.size.height);
[secondButton addTarget:self action:@selector(secondButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];

UIImage *thirdButtonImg = [UIImage imageNamed:(@"thirdButton")];
UIImage *thirdButtonImgHighlighted = [UIImage imageNamed:(@"thirdButtonHighlighted")];
self.thirdButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thirdButton setBackgroundImage:thirdButtonImg forState:UIControlStateNormal];
[thirdButton setBackgroundImage:thirdButtonImgHighlighted forState:UIControlStateHighlighted];
self.thirdButton.frame = CGRectMake(50, 120+aboutViewTop.frame.size.height+firstButton.frame.size.height+secondButton.frame.size.height, thirdButtonImg.size.width, thirdButtonImg.size.height);
[thirdButton addTarget:self action:@selector(thirdButtonEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdButton];