I am using autolayout with UIScrollView
to show some attributes from an object. I download this information dinamically from web service. The scrollview has a constant width (because I don't want to have a vertical scroll behavior) and its subviews respect this width with a group of constraints, but I can't to increase the UILabel
's height dynamically.
I code everything and I use viewDidLoad
selector to create subviews...
- (void)viewDidLoad {
[super viewDidLoad];
.
.
.
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
descriptionLabel.opaque = YES;
descriptionLabel.backgroundColor = [UIColor clearColor];
descriptionLabel.textColor = [UIColor whiteColor];
descriptionLabel.textAlignment = NSTextAlignmentRight;
descriptionLabel.font = [UIFont appetitoMediumItalicFontWithSize:15.0f];
descriptionLabel.text = NSLocalizedStringFromTable(@"APT_DISH_DETAIL_DESCRIPTION", @"DishDetail", @"Etiqueta que contiene la descripción del platillo");
[descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.detailContentScrollView addSubview:descriptionLabel];
self.descriptionLabelS = descriptionLabel;
.
.
.
}
You can watch the self.detailContentScrollView
variable, this is an IBOulet
created from view controller's nib.
Then I use the updateConstraints
selector...
- (void)updateConstraints {
[super updateConstraints];
// This dictionary has more variables, ok
NSDictionary *viewsDict = @{@"dish_description_label": self.descriptionLabelS};
.
.
.
[self.descriptionLabelS setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.detailContentScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view1][dish_description_label]-[view2][view3][view4]-|" options:0 metrics:nil views:viewsDict]];
.
.
.
}
and finally, when I receive the web service's info, I send sizeToFit
's UILabel
selector and layoutIfNeeded
from the scrollview. But my UILabel
never resizes itself with the new content. What am I doing wrong?
I had the same issue. There seems to be a bug with apple, so multiline text requires a two-pass approach to layout correct and it all relies on the property preferredMaxLayoutWidth.
I ended up adding these two methods to the ScrollViewController:
I based my solution on this answer: https://stackoverflow.com/a/13616052/2828256
If you want to make it with views:
Add a UIScrollview with a constraint height >= [minimum value you want] e.g. 480
Add a UILabel to the scrollview with a height constraint >= [minimum value you wan] e.g. 460
Like in the following images:
if you need to make it programmatically
UIScrollView content size is updated dynamically with autolayout, maybe you only must do the following
Where _contentView is your UILabel (If you have a more complex view hierarchy put on a view container) and self.view is the controller view (or anything else). Hope this helps also: iOS Autolayout with UIScrollview: Why does content view of scroll view not fill the scroll view?....
Also don't forget to establish your
UILabel preferredMaxLayoutWidth
Cheers!
Here is a full implementation:
For a detailed explanation:
https://arielelkin.github.io/articles/uilabel-plus-uiscrollview-plus-autolayout