【问题标题】:UITableViewCell's contentView gets unwanted "height==44" constraintUITableViewCell 的 contentView 获得不需要的“height==44”约束
【发布时间】:2014-11-23 20:31:18
【问题描述】:

我完全在代码中创建我的 UI,并使用 Masonry 将单元格的内容视图的子视图限制在适当的高度。我正在使用

[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height

在 iOS 7 上用于行高,而 iOS 8 会自动处理。

一切看起来都与屏幕上应有的完全一样,但在控制台中,我收到大量关于冲突约束的警告,这一切似乎都是由单元格内容视图上未询问且不必要的高度约束引起的(例如<NSLayoutConstraint UITableViewCellContentView.height == 44>)。

在 iOS 8 上,我将表视图的 rowHeight 设置为 UITableViewAutomaticDimension(实际上是 -1),但我仍然得到这个约束。我只是在内容视图和它自己的子视图之间添加约束,所以内容视图和单元格本身之间没有约束。

知道这个约束来自哪里以及如何让它消失吗?

编辑:现在我实际上找到了一种“解决方案”——在添加子视图或约束之前,最初将内容视图的框架设置为一些荒谬的东西,比如CGRectMake(0, 0, 99999, 99999),似乎让警告消失了离开。但这听起来不像是正确的方法,所以有人能说出更好的方法吗?

【问题讨论】:

    标签: ios uitableview autolayout


    【解决方案1】:

    我遇到了同样的问题并修复了它,像这样设置单元格的自动调整大小掩码:

    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.autoresizingMask = .flexibleHeight
    }
    

    我还在控制器中设置了估计高度并告诉表格视图使用自动尺寸(在 viewDidLoad 方法中:

        self.tableView.estimatedRowHeight = 120
        self.tableView.rowHeight = UITableView.automaticDimension
    

    这些链接有帮助:

    http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

    Auto layout constraints issue on iOS7 in UITableViewCell

    希望这会有所帮助!

    【讨论】:

    • 我有这个问题,并尝试了这个答案,但我仍然得到错误:(我正在使用 Snapkit btw。"<0x7f9d306a9720 myapp.custommessagetableviewcell:0x7f9d308b1c00.height="=">
    【解决方案2】:

    我发现在某些情况下,设置一个比我的平均单元格的高度大很多倍的估计高度可以修复大多数(如果不是所有)警告,并且对显示没有负面影响。

    即: 设置 self.tableView.estimatedRowHeight = 500.0f 而大多数行的高度只有大约 100.0f 解决了我的问题。

    【讨论】:

    • 这会导致滚动条在大型表格视图中“跳跃”,因为estimatedRowHeight 与实际大小有很大不同。
    【解决方案3】:
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    
            //self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
            self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
            self.itemView = [CustomItemView new];
            [self.contentView addSubview:self.itemView];
    
        }
    
        return self;
    }
    

    translatesAutoresizingMaskIntoConstraints 设置为NO 不适合我 , 但autoresizingMask = UIViewAutoresizingFlexibleHeight 很好。

    你也应该像这样进行约束:

    - (void)updateConstraints {
    
        [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
    
            make.leading.trailing.top.equalTo(0);
            //make.bottom.equalTo(0);
            make.bottom.lessThanOrEqualTo(0);
    
        }];
    
        [super updateConstraints];
    }
    

    底部约束不只是equalTo contentView 的底部,你应该使用lessThanOrEqualTo

    希望这对你有用!

    【讨论】:

    • 进一步说明:“ [LayoutConstraints] 不支持更改 UITableViewCell 的 contentView 的 translatesAutoresizingMaskIntoConstraints 属性,这将导致未定义的行为,因为此属性由拥有的 UITableViewCell 管理。” . (从控制台)。
    【解决方案4】:

    关于问题编辑中提到的“解决方案”(暂时将 contentView 框架设置为大的东西),这里证明这是一个很好的“解决方案”: https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/blob/master/TableViewCellWithAutoLayout/TableViewController/TableViewCell.swift

            // Note: if the constraints you add below require a larger cell size than the current size (which is likely to be the default size {320, 44}), you'll get an exception.
            // As a fix, you can temporarily increase the size of the cell's contentView so that this does not occur using code similar to the line below.
            //      See here for further discussion: https://github.com/Alex311/TableCellWithAutoLayout/commit/bde387b27e33605eeac3465475d2f2ff9775f163#commitcomment-4633188
            // contentView.bounds = CGRect(x: 0.0, y: 0.0, width: 99999.0, height: 99999.0)
    

    这很hacky,但似乎可以工作。

    【讨论】:

      【解决方案5】:

      为了接受接受的答案——在尝试让 iOS 8 的自动单元格大小调整几个月后,我发现了一个重要的警告。必须设置“estimatedRowHeight”属性。直接通过 tableView 或通过实现委托方法。即使无法确定有效的估计值,只要提供一个默认值 (0.0) 以外的值,iOS 8 的单元格布局就可以在我的测试中发挥作用。

      【讨论】:

      • 在使用 iOS 9 时似乎也必须设置estimatedRowHeight。
      猜你喜欢
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多