【发布时间】:2014-11-13 19:48:35
【问题描述】:
我正在尝试正确调整 UITableViewCell 的大小以适合 UILabel 和 UITextView,以便标签和 textView 中的所有文本都可见。
现在,我无法为 textView 获取合适的大小。这是我的布局代码:
if (_prototypeCell == nil) {
_prototypeCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DDDataDetectingDetailCell class]) owner:nil options:0] lastObject];
}
[_prototypeCell setFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 1)];
[_prototypeCell configureForItem:self.items[indexPath.row]];
[_prototypeCell setNeedsLayout];
[_prototypeCell layoutIfNeeded];
CGSize size = [_prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
现在,我已确保我的笔尖具有固定单元格 contentView 顶部的约束,向下穿过标签,最后到单元格底部。此外,单元格也具有沿水平维度连接的约束。但是,如果我打印出这个尺寸,我会得到以下信息:
(width=486.5, height=61.5)
这很奇怪,因为如果我打印出 textView 和单元格,我会得到:
(lldb) po _prototypeCell
<DDDataDetectingDetailCell: 0x79bd4700; baseClass = UITableViewCell; frame = (0 0; 320 1); autoresize = RM+BM; layer = <CALayer: 0x79bbba90>>
(lldb) po _prototypeCell.detailTextView
<UITextView: 0x7b9ffc00; frame = (15 1; 290 0); text = 'No need to pay -- Just le...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x79bce590>; layer = <CALayer: 0x79bd6910>; contentOffset: {0, 0}; contentSize: {290, 0}>
所以 textView 似乎是正确的宽度,但由于某种原因没有根据约束计算其高度。
这是我的 layoutSubviews 方法:
- (void)layoutSubviews {
[super layoutSubviews];
self.mainLabel.preferredMaxLayoutWidth = self.mainLabel.frame.size.width;
[super layoutSubviews];
}
这是我的约束的图片:
为什么我从-systemLayoutFittingSize: 得到这个时髦的宽度?
更新
我认为我错误地配置了 UITextView(当然是滚动视图)上的约束,因为滚动视图的 intrinsicContentSize 报告为 (width=456.5, height=32.5)
【问题讨论】:
-
您是否在单元格类中为您的 UITextView 设置了
self.textView.scrollEnabled = NO;? -
是的,我这样做了,不幸的是,它没有用。
-
你是用界面生成器做的吗?
标签: ios uitableview autolayout