UIImageView使用方面填充覆盖整个UITableViewCell

时间:2016-02-01 20:44:13

标签: ios swift uitableview uiimageview autolayout

在界面构建器中,我试图创建一个原型单元格,其中的图像覆盖整个单元格,但它没有按预期运行。

正如您在我的界面构建器的以下屏幕截图中所看到的,我有一个覆盖整个单元格的图像视图,并且被约束到单元格的每个边缘:

enter image description here

事实上,这就是我期望它在模拟器上的样子,但我得到了这个:

enter image description here

在你可以看到的地方,它并没有一直固定在两侧,而且可能很难看到,但是图像实际上延伸到了单元格的底部(如果你看起来很难看,你可以看到分隔符穿过图像的底部。

这真是个错误,我真的不知道发生了什么。

4 个答案:

答案 0 :(得分:1)

我认为您在代码或Storyboard中意外禁用了单元格Clip Subviews,默认情况下应启用它。

enter image description here

如果不是单元格,请检查Content View

enter image description here

顺便说一句,通过为Cell及其内容视图禁用Clip Subviews,我设法重现了您的错误。

答案 1 :(得分:1)

也许在代码中在单元格内添加UIImageView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //configure cell
    let imageView = UIImageView(frame: self.cell.frame)
    imageView.image = YOUR_IMAGE
    imageView.center = cell.center
    imageView.frame.size = cell.frame.size
    imageView.contentMode = .ScaleAspectFill
    cell.addSubview(imageView)
}

希望这会有所帮助。

答案 2 :(得分:0)

  1. 似乎您的图像约束与单元格contentView边距相关。你可以禁用它,看截图。确保常数为0
  2. enter image description here

    1. 如果您不希望纵横填充图像超出边界,则需要在单元格contentView或imageView上执行剪辑子视图(clipsToBounds)。否则,您应该使用Aspect Fit或Scale To Fill,或手动进行数学运算

答案 3 :(得分:0)

这是因为您将约束设置为边距

uiimageview添加约束时。取消选中约束边距。

enter image description here

相关问题