我已经按照本教程在 iOS8 中使用 UILabels 设置了自动调整单元格大小: http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
但是我在使用自动调整大小设置 UIImageView 时遇到了问题。与纵向相比,我需要图像的横向尺寸不同(因此它们保持相同的纵横比)。但是每次我都遇到布局约束错误
"<NSLayoutConstraint:0x14554680 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14564440(200)]>
由于实际代码有很多不同类型的单元格(它们都适用于自动调整单元格大小)我在这里设置了一个带有蓝色 ImageView 的简单示例:
https://github.com/AdrianMW/ios8autosizingCells
我已经设置了父 View 的约束并且正在设置高度:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kImageCell forIndexPath:indexPath];
cell.heightConstraint.constant = [self heightForOrientation:self.interfaceOrientation];
return cell;
}
尝试过的解决方案
我已尝试根据其他一些建议关闭单元格上的 setTranslatesAutosizingMask: 但当我这样做时,我只会得到一个空白区域而不是图像。我还尝试添加以下内容:
[cell setNeedsDisplay];
[cell layoutIfNeeded];
我看到有人提到覆盖 sizethatfits: 以使用旧方法让它在这个单元格上工作,但似乎没有在单元格上调用它。
干杯
最佳答案
在您的 ImageTableViewCell.xib 中,尝试更改约束:
Image View.Bottom EQUAL Superview.Bottom
将优先级从 1000 更改为 999。
关于objective-c - 如何在 iOS8 中使用 Autolayout 和 self-sizing cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27508617/