Reputation: 183
I have a UILabel
and a UIButton
inside of a Custom UIView. My UIButton
site below my UILabel
.
My UILabel
text however changes it's size and overlaps my UIButton
.
I want to be able to move my UIButton down when the UILabel resizes itself?
I only have managed to dynamically resize my UILabel
but have not been able to resize or move my 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); }
Upvotes: 0
Views: 848
Reputation: 8501
A couple of things...
I'm not sure why you are re-adding the UI components again when you say you've created them in IB? For example, you shouldn't need lines like the following if the controls are already in the view
[self.selectionsWrapper addSubview:self.chosenCategoriesLabel];
You also execute this line twice?
self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]);
To adjust the frame of your button do something like this...
// 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;
Depending on how you've defined you custom view you might also have to adjust the height of that as well to accommodate the new position of the button?
Upvotes: 1