2014-03-27 101 views
0

我在自定義UIView中有一個UILabel和一個UIButton。我的UIButton網站低於我的UILabel當UILabel動態調整大小時,在UIView中移動UIButton。 iOS

我的UILabel文本會改變它的大小並與我的UIButton重疊。

我想在UILabel調整自己的大小時能夠將UIButton向下移動嗎?

我只能設法動態調整我的UILabel的大小,但無法調整大小或移動我的UIButton

self.chosenCategoriesLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:17.0]; 
self.chosenCategoriesLabel.textColor = [UIColor colorWithRed:161.0/255.0 green:205.0/255.0 blue:86.0/255.0 alpha:1.0]; 
self.chosenCategoriesLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:15.0]; 
self.chosenCategoriesLabel.numberOfLines = 0; 
self.selectedCategoriesString = [self.selectedCategoriesArray componentsJoinedByString:@","]; 
self.chosenCategoriesLabel.lineBreakMode = NSLineBreakByWordWrapping; 
self.chosenCategoriesLabel.text = [self.selectedCategoriesString stringByAppendingString:@"\n\n\n\n"]; 
[self.selectionsWrapper addSubview:self.chosenCategoriesLabel]; 

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]); 

[self.itemThumbnail setImageWithURL:self.item.itemImageURL placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]]; 
[self.view addSubview:self.itemThumbnail]; 

[self.selectCategorieButton setTitle:@"Select Categories" forState:UIControlStateNormal]; 
[self.selectCategorieButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[self.selectCategorieButton setTitle:@"Select Categories" forState:UIControlStateHighlighted]; 
[self.selectCategorieButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 
self.selectCategorieButton.titleLabel.font = lightHelveticaFont; 
[self.selectCategorieButton setBackgroundImage:[UIImage imageNamed:@"btn_choose_categories.png"] forState:UIControlStateNormal]; 
[self.selectCategorieButton setBackgroundImage:[UIImage imageNamed:@"btn_choose_categories.png"] forState:UIControlStateHighlighted]; 
[self.selectCategorieButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]]; 
[self.selectCategorieButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:15.0]]; 
[self.selectionsWrapper addSubview:self.selectCategorieButton]; 

[self changeLabel]; 

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]); 

-(void)changeLabel { 

self.chosenCategoriesLabel = [NSString stringWithFormat:@"%@",array]; 

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]); 

} 

- (CGFloat) sizeLabel 
{ 
#pragma clang diagnostic push 
#pragma clang diagnostic ignored "-Wdeprecated-declarations" 
CGSize sizeToFit = [self.chosenCategoriesLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(276.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 
#pragma clang diagnostic pop 
return fmaxf(22.0f, sizeToFit.height + 40); } 
+0

這是你的初始化代碼嗎?你能否展示你調整你的標籤的部分? – streem

+0

你在哪裏創建按鈕和標籤?因爲你似乎通過代碼將它們添加到視圖中? – Flexicoder

+0

我在界面生成器中創建了uibutton和uilabel。我只更改我的代碼 – CapeFurSeal

回答

1

幾件事情......

我不知道你爲什麼要重新添加UI組件時再你說你在IB創建它們?例如,如果控件已經位於視圖中,則不應該需要以下行:

[self.selectionsWrapper addSubview:self.chosenCategoriesLabel]; 

您也執行此行兩次嗎?

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]); 

要調整按鈕的邊框做這樣的事......

// Get the original frames; 
CGRect buttonFrame = self.selectCategorieButton.frame; 
CGRect labelFrame = self.chosenCategoriesLabel.frame; 

//Work out the new height 
CGFloat newHeight = [self sizeLabel]; 

//Work out what the difference is 
CGFloat adjustment = newHeight - labelFrame.size.height; 

//Set the labels new height 
labelFrame.size.height = newHeight; 
self.chosenCategoriesLabel.frame = labelFrame; 

//add the difference to the buttons frame 
buttonFrame.origin.y = buttonFrame.origin.y + adjustment; 
self.selectCategorieButton.frame = buttonFrame; 

取決於你如何定義你的自定義查看您可能還需要調整的高度,以及以適應按鈕的新位置?