iOS - 表格视图单元格中的Autolayout问题

时间:2018-04-14 17:05:35

标签: ios uitableview autolayout

我有一个包含以下项目的表格视图单元格:

  • 图片浏览(343 x 193)
  • 标签1(身高:17)
  • 标签2(身高:20)

无论设备是什么,我希望图像保持相同的宽高比(此应用仅适用于iPhone,而不适用于iPad)。我正在做的是添加以下内容:

  • 图像视图:纵横比约束;顶部,左侧,右侧约束

  • 标签1:高度限制;顶部(关于图像视图),左,
    正确的约束;等宽(带图像视图)

  • 标签2:高度限制;顶部(关于图像视图),左,
    右,底(关于细胞)约束;宽度相等(用 图片视图)

当我测试这个时,我得到以下

2018-04-14 11:50:33.457531-0500 MyAppIOS[4357:479920] [LayoutConstraints] 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:0x60000009f9a0 UIImageView:0x7ff04952dfb0.width == 1.7772*UIImageView:0x7ff04952dfb0.height   (active)>",
    "<NSLayoutConstraint:0x60000009fc70 UILabel:0x7ff04952e1e0.height == 17   (active)>",
    "<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>",
    "<NSLayoutConstraint:0x60000009fea0 V:|-(10)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009fef0 H:[UIImageView:0x7ff04952dfb0]-(16)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff40 H:|-(16)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff90 V:[UIImageView:0x7ff04952dfb0]-(8)-[UILabel:0x7ff04952e1e0]   (active)>",
    "<NSLayoutConstraint:0x600000280140 V:[UILabel:0x7ff04952e1e0]-(8)-[UILabel:0x7ff04952e940'Loading...']   (active)>",
    "<NSLayoutConstraint:0x6000002801e0 V:[UILabel:0x7ff04952e940'Loading...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60800009c070 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff04952d770.height == 266   (active)>",
    "<NSLayoutConstraint:0x60800009e230 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff04952d770.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>

所以基本上它说它会修改第二个标签的高度以适应。

有没有更好的方法来设置这些约束?我应该比我正在使用的更多/更少吗?

1 个答案:

答案 0 :(得分:0)

冲突背后的原因是单元格最初假定静态高度约束与子视图中的当前约束设置冲突。从UIView-Encapsulated-Layout-Height

可以清楚地看出这一点

因此,将label2的底部约束的优先级设置为999。

(另外,如果设置左右约束,请不要设置宽度约束。)