我正在使用自动布局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]];
.
.
.
}
最后,当我收到网络服务的信息时,我会发送sizeToFit
选择UILabel
器并layoutIfNeeded
从滚动视图中发送。但我UILabel
从来没有用新内容调整自己的大小。我究竟做错了什么?