【问题标题】:UITableViewCell Image full-size with right aspect ratio具有正确纵横比的 UITableViewCell 图像全尺寸
【发布时间】:2018-08-28 02:52:20
【问题描述】:

我创建了一个 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.

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

【问题讨论】:

    标签: objective-c xcode interface-builder constraints


    【解决方案1】:

    问题在于,只要您的像元大小没有 8:6 的比例,您的约束就会发生冲突。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2016-08-06
      • 2019-04-12
      • 2012-02-24
      相关资源
      最近更新 更多