我正在尝试使用AutoLayout在我的表格视图单元格中配置子视图,在理想情况下,希望表格视图单元格尽可能高以包含所有子视图。但是,这似乎是不可能的,因为在实际创建像元之前就确定了像元的高度。
因此,到目前为止,我只是查看了设置的约束条件并计算了包含所有内容所需的总高度,并将其在情节提要中设置为该特定TableViewCell原型单元格的行高,并且还在tableView:heightForRowAtIndexPath:方法中返回了此高度。
现在,我的表格视图单元格如下所示(情节提要的屏幕截图):
有两个没有显示大小的约束,它们都具有大小10(按钮顶部容器视图以及滑块和中间标签之间的距离)。
从上到下的距离为:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x8d80240 V:[UISlider:0x8d81190(30)]>",
"<NSLayoutConstraint:0x8d833e0 V:[UILabel:0x8d83300(20)]>",
"<NSLayoutConstraint:0x8d83750 V:[UIButton:0x8d83600(50)]>",
"<NSLayoutConstraint:0x8d85050 V:|-(10)-[UIButton:0x8d83600] (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d851d0 V:[UILabel:0x8d83300]-(10)-[UISlider:0x8d81190]>",
"<NSLayoutConstraint:0x8d85200 V:[UISlider:0x8d81190]-(20)-| (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d85320 V:[UIButton:0x8d83600]-(20)-[UILabel:0x8d83300]>",
"<NSAutoresizingMaskLayoutConstraint:0x8d8b7a0 h=--& v=--& V:[UITableViewCellContentView:0x8d80ef0(161)]>"
tableView:heightForRowAtIndexPath:方法返回160:-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == self.fetchedItemSetsController.fetchedObjects.count - 1)
{
return 160;
}
return 44;
}
"<NSLayoutConstraint:0x17809ce80 V:[UISlider:0x125613660(30)]>",
"<NSLayoutConstraint:0x1782814f0 V:[UILabel:0x125615100(20)]>",
"<NSLayoutConstraint:0x178281a90 V:[UIButton:0x125615b50(50)]>",
"<NSLayoutConstraint:0x178281b80 V:|-(10)-[UIButton:0x125615240] (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281c20 UIButton:0x1256157b0.height == UIButton:0x125615240.height>",
"<NSLayoutConstraint:0x178281d10 UIButton:0x1256157b0.height == UIButton:0x125615980.height>",
"<NSLayoutConstraint:0x178281e00 V:[UILabel:0x125615100]-(10)-[UISlider:0x125613660]>",
"<NSLayoutConstraint:0x178281e50 V:[UISlider:0x125613660]-(20)-| (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281f40 UIButton:0x125615980.height == UIButton:0x125615b50.height>",
"<NSLayoutConstraint:0x178282030 V:[UIButton:0x125615240]-(20)-[UILabel:0x125615100]>",
"<NSAutoresizingMaskLayoutConstraint:0x178283340 h=--& v=--& V:[UITableViewCellContentView:0x178161980(159.5)]>"
tableView:heightForRowAtIndexPath:的返回值的奇怪组合。tableView:heightForRowAtIndexPath:方法的返回值(方法)所生成的autoresizingmasklayoutconstraint高度的一些示例。在所有情况下,子视图的约束都应导致高度为160,并且不得更改。最佳答案
Separator弄乱了contentView的高度,要么应该禁用它(如果需要,可以用自定义的替换),以便contentView的高度与cell的高度匹配,或者应该考虑到contentView的高度,将约束更改为更灵活与cell的高度不匹配。两者都做会更好。
关于ios - 使用NSAutoresizingMaskLayoutConstraint进行UITableViewCell舍入错误,但是在Storyboard和heightForRowAtIndexPath中正确设置了大小:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22539994/