UITableViewCell图像全尺寸,具有正确的宽高比

时间:2018-03-19 13:57:33

标签: objective-c xcode interface-builder constraints

我创建了一个tableviewcell。在它是一个图像。我想在列的整个宽度上显示图像。 为此,我添加了所有设置为零的约束: - 领先的空间 - 尾随空间 - 顶层空间 - 底部空间 我还定义了一个纵横比约束8:6来获得图像的正确宽高比。

在Interface Builder中,我得到了"红色箭头"错误"需要约束:X的宽度位置"。

当我运行代码(它是可编译的)时,它显示得很好。但是我得到运行时约束警告:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60800008cfd0 UILayoutGuide:0x6080001a5080'UIViewSafeAreaLayoutGuide'.bottom == UIImageView:0x7fc9fa90fa10.bottom + 8   (active)>",
    "<NSLayoutConstraint:0x60800008d2a0 UIImageView:0x7fc9fa90fa10.centerY == UILayoutGuide:0x6080001a5080'UIViewSafeAreaLayoutGuide'.centerY   (active)>",
    "<NSLayoutConstraint:0x60800008d2f0 UIImageView:0x7fc9fa90fa10.top == UILayoutGuide:0x6080001a5080'UIViewSafeAreaLayoutGuide'.top + 7   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800008d2a0 UIImageView:0x7fc9fa90fa10.centerY == UILayoutGuide:0x6080001a5080'UIViewSafeAreaLayoutGuide'.centerY   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我看不出我做错了什么,有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

问题是,只要你的单元格大小没有8:6的比例,你的约束就会发生冲突。

有几种方法可以解决此问题,但我认为最简单的方法是使用leading约束替换trailingcenter horizontally约束。图像现在将居中,其高度由单元格的高度设置,其宽度将由纵横约束设置。唯一需要注意的是,如果单元格变得比8:6更窄,那么图像将延伸到左/右边缘之外。

相关问题