【问题标题】:How to use Autolayout and self-sizing cells in iOS8如何在 iOS 8 中使用自动布局和自动调整单元格大小
【发布时间】:2014-12-16 15:54:39
【问题描述】:

按照本教程,我在 iOS8 中使用 UILabel 设置了自动调整大小的单元格,没有问题: http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

但我在使用自动调整大小设置 UIImageView 时遇到问题。与纵向相比,我需要横向的图像尺寸不同(因此它们保持相同的纵横比)。但是每次我得到一个损坏的布局约束错误

"<NSLayoutConstraint:0x14554680 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14564440(200)]>

由于实际代码有很多不同类型的单元格(都适用于自动调整大小的单元格),我在此处设置了一个带有蓝色图像视图的简单示例:

https://github.com/AdrianMW/ios8autosizingCells

我已经设置了对超级视图的约束,并且正在设置高度,如下所示:

- (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: 让它使用旧方法在这个单元格上工作,但似乎没有在单元格上调用。

干杯

【问题讨论】:

    标签: objective-c uitableview autolayout


    【解决方案1】:

    在您的 ImageTableViewCell.xib 中,尝试更改约束:

    Image View.Bottom EQUAL Superview.Bottom
    

    将优先级从 1000 更改为 999。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      相关资源
      最近更新 更多