【问题标题】:AutoLayout in UITableViewCell with UITextView带有 UITextView 的 UITableViewCell 中的自动布局
【发布时间】: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


【解决方案1】:

我认为您需要将标签和文本视图上的内容压缩阻力设置为 1000(必需)。发生的事情是标签和文本视图正在压缩它的内容。

您可能还需要在文本视图上创建高度约束并将其设置为文本视图的 contentSize。

【讨论】:

  • 也试过了。我可以让它工作的唯一方法是子类化并更改内在内容大小
猜你喜欢
  • 2015-09-12
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多