iOS 8:UITableViewCell.contentView不调整其中的子视图

我把自定义视图放在UITableViewCell.contentView ,将自定义视图的自动autoresize mask设置为W+H

但运行时, CustomViewContentView获得更高的高度, ContentView高度是60pt (与UITableViewDelegate设置相同),但CustomView内部的76pt高度为76pt

通过Xcode6's view debugging ,我发现我的自定义视图有一些奇怪的约束,它们是:

 self.height = superview.height + 16 self.midY = superview.midY + 8 

这些制约因素来自哪里以及如何修改它们? 我从来没有设置任何值8或16。

更新:

我做了一个testing项目,这是一个简单的故事板中的一个CustomView从一个nib文件加载一个CustomView ,这个testing项目复制了问题,运行时,作为TableViewCell.contentView子视图的CustomView变得比TableViewCell.contentView

testing项目位于: https : //drive.google.com/file/d/0B5y_NrRbhGlSb1dlbVZNb19vNjQ/view?usp =分享

最后我明白,自动布局只能在相同的xib或storyboard中形成关系,我的CustomView是在一个分离的xib中,并在运行时加载,所以超级视图和CustomView之间没有任何自动布局约束。

如果我设置translatesAutoresizingMaskIntoConstraints为YES,那么出现这个问题,我仍然不知道为什么使用W + H作为自动resize的掩码使得CustomView比它的超级视图(cell.contentView)高,但是我find了一个解决方法:

我在superview和CustomView之间手动添加约束,并closurescustomView. translatesAutoresizingMaskIntoConstraints customView. translatesAutoresizingMaskIntoConstraints自动化仓库的内部约束,代码如下:

  customView.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]; NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:0]; [cell.contentView addConstraints:@[top, bottom, left, right]];