动态的UILabel内UIScrollView的高度与自动布局 [英] Dynamic UILabel height inside UIScrollView with Autolayout
问题描述
我使用的自动布局与的UIScrollView
显示从对象的某些属性。我从Web服务dinamically下载此信息。滚动视图具有恒定的宽度(因为我不希望有一个垂直滚动行为)及其子视图尊重这个宽度与一组约束,但我不能增加的UILabel
的高度动态。
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.
我code一切,我用 viewDidLoad中
选择创建子视图...
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;
.
.
.
}
您可以观看 self.detailContentScrollView
变量,这是一个 IBOulet
从视图控制器的笔尖创建的。
You can watch the self.detailContentScrollView
variable, this is an IBOulet
created from view controller's nib.
然后我使用 updateConstraints
选择...
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]];
.
.
.
}
最后,当我收到Web服务的信息,我送 sizeToFit
的的UILabel
选择和 layoutIfNeeded
从滚动视图。但我的的UILabel
从来没有新的内容调整大小本身。我在做什么错了?
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?
推荐答案
UIScrollView的内容大小与自动布局动态更新,也许你只是必须执行以下操作
UIScrollView content size is updated dynamically with autolayout, maybe you only must do the following
- (void) setupScroll
{
[_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_scrollView addSubview:_contentView];
NSArray *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_contentView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_contentView)];
NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_contentView)];
[_scrollView addConstraints:horizontal];
[_scrollView addConstraints:vertical];
UIView *mainView = self.view;
horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_contentView(==mainView)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_contentView, mainView)];
[mainView addConstraints:horizontal];
}
在哪里_contentView是你的UILabel(如果你已经把一个更复杂的视图层次视图上的容器),并self.view是控制器视图(或其他)。希望这有助于也是:<一href=\"http://stackoverflow.com/questions/16843633/ios-autolayout-with-uiscrollview-why-does-content-view-of-scroll-view-not-fill\">iOS自动布局用的UIScrollView?为什么滚动视图的内容视图不填滚动视图 ....
另外,不要忘记建立您的UILabel preferredMaxLayoutWidth
Also don't forget to establish your UILabel preferredMaxLayoutWidth
干杯!
这篇关于动态的UILabel内UIScrollView的高度与自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!