objective-c - 如何在 iOS8 中使用 Autolayout 和 self-sizing cells

标签 objective-c uitableview autolayout

我已经按照本教程在 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/

相关文章:

objective-c - 创建具有不同子类属性的子类

iOS:dismissViewControllerAnimated 在执行时崩溃

ios:在 iphone6 上运行时增加 UIViewController 中自定义 View 的宽度

uiview - systemLayoutSizeFittingSize : require setNeedsLayout & layoutIfNeeded before it?

ios - 如何使用 Facebook PoP ios 框架水平翻转 UIView?

objective-c - +[NSString stringWithString :] -- what's the point?

swift - 设置高度: TableViewCells with custom Nib that are different sizes

iphone - UITableView 动态添加单元格

ios - UITableViewCell 在呈现时自动调整大小

ios - 已经为 iPhone 4 和 5 构建的应用程序将如何在 iPhone 6 和 6 plus 中展示