[英]Autolayout, View Height depending on subviews height
Using autolayout I'm developing a container UIView which contains a UILabel. 使用autolayout我正在开发一个包含UILabel的容器UIView。 I want to make the hight of the container view dynamically depending on the height of the UILabel. 我想根据UILabel的高度动态地制作容器视图的高度。
I'm adding these constraints inside the container view, but I don't know how to tell the height of the UILabel because it's a multiline UILabel. 我在容器视图中添加了这些约束,但我不知道如何判断UILabel的高度,因为它是一个多行UILabel。
- (void) updateConstraints{
[self removeConstraints:self.constraints];
NSDictionary *views = NSDictionaryOfVariableBindings(_informationLabel);
// -- Constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_informationLabel]-5-|"
options:0
metrics:@{}
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_informationLabel]-5-|"
options:0
metrics:@{}
views:views]];
[super updateConstraints];}
I've tried calculating the height with boundingRectWithSize
and sending the height inside the metrics dictionary. 我已经尝试使用boundingRectWithSize
计算高度并在metrics字典中发送高度。
In the ViewController I haven't specify the height of the container view to let it resize depending on the height of subviews: 在ViewController中,我没有指定容器视图的高度,以便根据子视图的高度调整大小:
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[_informationView]"
options:0
metrics:@{}
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_informationView]-|"
options:0
metrics:@{}
views:views]];
How I can make the container view height grow as uilabel height has to grow? 如果uilabel高度必须增长,我怎样才能使容器视图高度增加?
The intrinsicContentSize
of multi-line text ( UILabel
& NSTextView
) is ambiguous. 所述intrinsicContentSize
多行文本的( UILabel
& NSTextView
)是不明确的。 This is because the height depends on the width which hasn't yet been calculated. 这是因为高度取决于尚未计算的宽度。 To solve this, you have to allow a first pass of layout, and then set the newly added property preferredMaxLayoutWidth
which solves this problem specifically. 要解决此问题,您必须允许第一次布局,然后设置新添加的属性preferredMaxLayoutWidth
,它具体解决了这个问题。
// Inside superview
- (void)layoutSubviews
{
[super layoutSubviews];
self.informationLabel.preferredMaxLayoutWidth = self.informationLabel.frame.size.width;
[super layoutSubviews];
}
Source: http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html 资料来源: http : //www.objc.io/issue-3/advanced-auto-layout-toolbox.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.