Mar*_*k W 5 iphone objective-c uitableview ios autolayout
我正在使用子类UITableViewCell,我需要在使用自动布局时将UILabel的文本对齐到左上角.我意识到读取sizeToFit确实不应该与自动布局一起使用,我想避免它,并以某种方式使用约束.基本上,每次重复使用单元时都会重置标签的文本,因此在重用时,大小调整需要是动态的.
这是子类化单元格内的Lazy初始化程序标签:
- (UILabel *)commentsLabel {
if (_commentsLabel == nil) {
_commentsLabel = [[UILabel alloc] init];
[_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
_commentsLabel.numberOfLines = 0;
_commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
_commentsLabel.preferredMaxLayoutWidth = 100;
}
return _commentsLabel;
}
Run Code Online (Sandbox Code Playgroud)
标签上设置了自动布局约束(commentsLabel是添加到单元子类上的self.customView的子视图):
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
options:0
metrics:@{
@"locationToTopPadding":@(locationToTopPadding),
@"locationHeight":@(locationHeight),
@"locationToCommentsPadding":@(locationToCommentsPadding),
@"commentsToBottomPadding":@(commentsToBottomPadding)
}
views:viewsDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
options:0
metrics:@{@"commentsWidth":@(commentsWidth)}
views:viewsDictionary]];
Run Code Online (Sandbox Code Playgroud)
设置只是:
self.commentsLabel.preferredMaxLayoutWidth = 100;
Run Code Online (Sandbox Code Playgroud)
虽然在大多数答案中提到过,但似乎没有用.
目前有这个实施但没有看到任何结果. UILabel sizeToFit不适用于autolayout ios6
我试过的另一个答案. UILabel sizeToFit仅适用于关闭AutoLayout
我觉得我只缺少1个约束,除了上面的约束之外还可以添加,但我尝试添加的程序性约束不起作用或抛出异常.我完全在代码中工作没有xib.
ETA:尝试在现有垂直约束线内设置高度约束:
像这里建议:动态改变UILabel宽度不适用于autolayout
在上面的垂直约束线中:
-[commentsLabel(>=30@900)]-
Run Code Online (Sandbox Code Playgroud)
我已经弄乱了高度值和优先级值,没有任何改变.
ETA:取得一些进展,我认为它与标签的底部填充有关,试过这个并且一些标签正确对齐有些不是:
->=commentsToBottomPadding-
Run Code Online (Sandbox Code Playgroud)
如果其他人遇到与 UITableViewCell 和动态更改的自定义标签相同的问题,则解决方案:
-[commentsLabel]->=yourBottomPadding-
Run Code Online (Sandbox Code Playgroud)
把它放在垂直约束中
设置文本后在 viewController 中调用它:
[cell updateConstraints];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4146 次 |
| 最近记录: |