当UILabel动态调整大小时,在UIView中移动UIButton。 iOS
问题描述:
我在自定义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); }
答
几件事情......
我不知道你为什么要重新添加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;
取决于你如何定义你的自定义查看您可能还需要调整的高度,以及以适应按钮的新位置?
这是你的初始化代码吗?你能否展示你调整你的标签的部分? – streem
你在哪里创建按钮和标签?因为你似乎通过代码将它们添加到视图中? – Flexicoder
我在界面生成器中创建了uibutton和uilabel。我只更改我的代码 – CapeFurSeal