我使用自动布局与UIScrollView
来显示对象的某些属性。我从网络服务中免费下载这些信息。 scrollview有一个不变的宽度(因为我不想有垂直滚动行为),它的子视图用一组约束来尊重这个宽度,但是我不能动态增加UILabel
的高度。使用Autolayout在UIScrollView中动态UILabel高度
我的代码一切,我用viewDidLoad
选择创建子视图...
- (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
。
然后我用updateConstraints
选择...
- (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
从来没有调整自己的新内容。我究竟做错了什么?
这是对这个问题的另一个功能的解决方案,谢谢。 – specktro
检查这个职位的完整实施:http://arielelkin.github.io/articles/uilabel-plus-uiscrollview-plus-autolayout/ – Eric
@Eric:链接关闭。 – testing