2015-03-03 63 views
0

我創建與在Xcode 3個按鈕的圖,該代碼如下:如何距設置爲這些UIButtons

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]; 

但這些按鈕是緊密放置,而不20空間我在它們之間加入,我想不通爲什麼

請幫我

+0

請捫心自問,如果你真的需要做的代碼UI工作,而不是使用界面生成器。使用自動佈局可以節省數小時和數小時的努力來解決這類問題。 – 2015-03-03 13:39:56

回答

0

對不起你們

問題解決

看來我不能添加號碼RECT多次

權代碼:

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]; 
0

在第一種情況下嘗試此

float yValue = 60+self.aboutViewTop.frame.size.height+20; 
self.firstButton.frame = CGRectMake(50, yValue, firstButtonImg.size.width, firstButtonImg.size.height); 
yValue = CGRectGetMaxY(self.firstButton.frame)+20; //y+height of first button+20 spacing 
self.secondButton.frame = CGRectMake(50, yValue, secondButtonImg.size.width, secondButtonImg.size.height); 
yValue = CGRectGetMaxY(self.secondButton.frame)+20; 
self.thirdButton.frame = CGRectMake(50, yValue, thirdButtonImg.size.width, thirdButtonImg.size.height); 
0
self.firstButton.frame = CGRectMake(50, 60+self.aboutViewTop.frame.size.height+20, firstButtonImg.size.width, firstButtonImg.size.height); 

:60 + 20 + topViewHeight

self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height); 

你會得到: 60 + 20 + topViewHeight + firstButtonHeight

所以沒有利潤空間。 我認爲你必須添加20到第二個按鈕框架的第一個:

self.secondButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+**20.f**+firstButton.frame.size.height+20.f, secondButtonImg.size.width, secondButtonImg.size.height); 

和40到第三:

self.thirdButton.frame = CGRectMake(50, 60+aboutViewTop.frame.size.height+firstButton.frame.size.height+**40.f**+secondButton.frame.size.height+20.f, thirdButtonImg.size.width, thirdButtonImg.size.height);