sim*_*mon 30 ios autolayout ios6
我有一个只包含UILabel的视图.此标签包含多行文字.父级具有可变宽度,可以使用平移手势调整大小.我的问题是,当我这样做时,调整UILabel的大小不会重新计算其高度,使得所有内容仍然可见,它只是将其切断.
我已经设法通过一些黑客来修复它但运行速度非常慢:
- (void)layoutSubviews {
CGSize labelSize = [self.labelDescription sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)];
if (self.constraintHeight) {
[self removeConstraint:self.constraintHeight];
}
self.constraintHeight = [NSLayoutConstraint constraintWithItem:self.labelDescription attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:labelSize.height];
[self addConstraint:self.constraintHeight];
[super layoutSubviews];
}
Run Code Online (Sandbox Code Playgroud)
这不应该自动布局自动发生吗?
编辑
我的观点结构是:
UIScrollView
---> UIView
---> UILabel
Run Code Online (Sandbox Code Playgroud)
以下是UIScrollView的约束:
<NSLayoutConstraint:0x120c4860 H:|-(>=32)-[DescriptionView:0x85c81c0] (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48a0 H:|-(32@900)-[DescriptionView:0x85c81c0] priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48e0 H:[DescriptionView:0x85c81c0(<=576)]>,
<NSLayoutConstraint:0x120c4920 H:[DescriptionView:0x85c81c0]-(>=32)-| (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c4960 H:[DescriptionView:0x85c81c0]-(32@900)-| priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x8301450 DescriptionView:0x85c81c0.centerX == UIScrollView:0x85db650.centerX>,
Run Code Online (Sandbox Code Playgroud)
以下是UIView的约束:
<NSLayoutConstraint:0x85c4580 V:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>,
<NSLayoutConstraint:0x85c45c0 H:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>,
<NSLayoutConstraint:0x85c9f80 UILabel:0x85bc7b0.trailing == DescriptionView:0x85c81c0.trailing>,
<NSLayoutConstraint:0x85c9fc0 UILabel:0x85bc7b0.centerY == DescriptionView:0x85c81c0.centerY>
Run Code Online (Sandbox Code Playgroud)
UILabel本身没有任何限制,除了将其固定到其父级的边缘
mat*_*att 41
好的,我终于把它钉了下来.解决方案是设置preferredMaxLayoutWidthin viewDidLayoutSubviews,但仅在第一轮布局之后.您可以通过异步调度回主线程来安排此操作.所以:
- (void)viewDidLayoutSubviews {
dispatch_async(dispatch_get_main_queue(), ^{
self.theLabel.preferredMaxLayoutWidth = self.theLabel.bounds.size.width;
});
}
Run Code Online (Sandbox Code Playgroud)
这样,preferredMaxLayoutWidth直到标签的宽度被其与superview相关的约束正确设置后才能设置.
这里的工作示例项目:
编辑:另一种方法!子类UILabel并覆盖layoutSubviews:
- (void) layoutSubviews {
[super layoutSubviews];
self.preferredMaxLayoutWidth = self.bounds.size.width;
}
Run Code Online (Sandbox Code Playgroud)
结果是一个自定尺寸的标签 - 它自动改变其高度以适应其内容,无论其宽度如何变化(假设其宽度由约束/布局改变).
sim*_*mon 26
我在提出Apple错误后解决了这个问题.多行文本需要两遍布局的问题,这一切都依赖于属性preferredMaxLayoutWidth
以下是需要添加到包含多行标签的视图控制器的相关代码:
- (void)viewWillLayoutSubviews
{
// Clear the preferred max layout width in case the text of the label is a single line taking less width than what would be taken from the constraints of the left and right edges to the label's superview
[self.label setPreferredMaxLayoutWidth:0.];
}
- (void)viewDidLayoutSubviews
{
// Now that you know what the constraints gave you for the label's width, use that for the preferredMaxLayoutWidth—so you get the correct height for the layout
[self.label setPreferredMaxLayoutWidth:[self.label bounds].size.width];
// And then layout again with the label's correct height.
[self.view layoutSubviews];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21339 次 |
| 最近记录: |