2014-09-29 113 views
15

我在代码中完全创建我的UI并使用Masonry将单元格的内容视图的子视图约束到适当的高度。我正在使用UITableViewCell的contentView获取不需要的“height == 44”约束

[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height 

iOS 7为行高,而iOS 8自动处理它。

一切看起来应该与屏幕上显示的一样,但在控制台中,我得到了有关冲突约束的警告列表,这些警告似乎都是由单元格内容视图上未激活的和不必要的高度约束(例如<NSLayoutConstraint UITableViewCellContentView.height == 44>)引起的。

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

任何想法,这个约束来自哪里,以及如何使它消失?

编辑:现在我居然找到了各种各样的“解决方案” - 内容视图的框架最初设置的东西可笑,像CGRectMake(0, 0, 99999, 99999),添加子视图或约束之前,似乎使警告消失。但是,这并不完全嗅觉到正确的方式,所以任何人都可以告诉更好的方法?

回答

16

我有同样的问题,并修复它设定该单元的这样的自动调整大小面膜:

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

而且在我设定的预测的高度,并告诉表视图使用自动尺寸控制器(以viewDidLoad方法:

self.tableView.estimatedRowHeight = 120 
    self.tableView.rowHeight = UITableViewAutomaticDimension 

这些链接帮助:

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

Auto layout constraints issue on iOS7 in UITableViewCell

希望这有助于!

+0

我有这个问题,并尝试这个答案,但我仍然得到错误:(我使用Snapkit BTW “”。 – 2016-03-04 20:48:37

+0

每HTTPS: //github.com/Alex311/TableCellWithAutoLayout/commit/bde387b27e33605eeac3465475d2f2ff9775f163#commitcomment-4633188这不是一个最佳的解决方案,请参阅我的解决方案,而不是 – 2016-06-03 00:38:29

+1

只设置估计行高解决了我的问题self.tableView.estimatedRowHeight = 120谢谢Maricel – crazywood 2016-08-18 07:53:21

5

为了解决接受的答案 - 在试图让iOS 8的自动单元大小工作几个月后,我发现了一个重要的警告。 'estimatedRowHeight'属性必须设置。通过tableView直接或通过实现委托方法。即使没有办法确定一个有效的估计值,只需提供一个非默认值(0.0)的值,就可以证明iOS 8的单元布局可以在我的测试中工作。

+1

看起来在使用iOS 9时也必须设置RowHeight。 – CodeReaper 2016-06-13 12:29:45

4

关于在问题在编辑中提到的“解决方案”(设置内容查看帧大的东西是暂时的),这里的证明,这是一个很好的“解决方案”: 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) 

这是哈克,但它似乎工作。

0
- (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; 
} 

设置translatesAutoresizingMaskIntoConstraintsNO不适合我 工作,但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内容查看的底部,你应该使用lessThanOrEqualTo

希望这是工作给你!

0

我发现,在某些情况下,将平均单元高度设置的估计高度设置为大多数(如果不是全部)警告并且对显示没有负面影响,那么该高度就会大很多倍。

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

+0

然而,这会产生滚动条以在大型浏览器中“跳跃”,因为estimatedRowHeight与实际尺寸大不相同。 – Daniel 2018-02-19 15:36:34