Autolayout - 带有 ImageView 的 UITableViewCell - 边距?

Autolayout - UITableViewCell with ImageView - Margin?

当我尝试在单元格中使用 UIImageView 创建 left/top/bottom 边距时遇到问题。它不关心我将尝试设置什么 Top/Bottom/Left Space,我总是会得到相同的结果 -> 所有图像都没有任何边距。

我有固定的高度和宽度,我只想添加 top/left 边距。

有什么办法可以强制Autolayout在这里加边距吗?

提前感谢您提供任何提示。 编辑: 谢谢你的回答。我试过了,但它仍然无法正常工作。单元格和图像之间始终没有边距:

这是我的限制条件。 (我只是把 8px 作为 space 到 superView)

实际上我无法完全解决这个问题。但我相信如果您在代码中尝试这些东西,它会起作用。

Using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights

编辑:

将新的 UIImageView 大小和位置设置为下面屏幕截图中显示的值。

现在 select UIImageView 并添加以下约束。

leadingtopbottom 设置为 20。
宽度高度 设置为 100。
确保未选中 限制到边距
点击添加 5 个约束 按钮。

Select image view 显示其约束,然后 select 其 bottom 约束对其进行编辑。在属性编辑器中,将其 Relation 更改为 Greater Than or Equal 并将其 Priority 更改为 999.

接下来,select image view 显示其约束,然后 select 其 height 约束。在属性编辑器中,将其 Priority 更改为 999。
同样,select图像视图的宽度约束并将其优先级更改为999。

要设置以下约束,你会得到答案。

cellForRowAtIndexPath

中添加此代码


 UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];/// change size as you need.
    CGRect frame = separatorLineView.frame;
    frame.size.height = 5.0;
    frame.size.width = tableView.frame.size.width;
    separatorLineView.frame = frame;
    separatorLineView.backgroundColor = [UIColor redColor];

    [cell.contentView addSubview:separatorLineView];
    return cell;


点击这些链接 Autolayout or Autolayout programatically

现在使用这些约束

故事板

模拟器

这可能对你有帮助:)