Autolayout - 无法满足约束

时间:2016-02-16 22:54:40

标签: ios autolayout ios-autolayout

更新:所以我尝试添加堆栈视图并注意到一个有趣的现象。似乎当我有最后一个图像视图或堆栈视图的边缘有superview = -1(Xcode默认值)的后沿时,它会拉伸所有内容并打破图像。但是,如果我没有这个约束,它不会在细胞上均匀分布,但所有图像都适合......我如何实现两者的组合!!!

好的,只是为了序言,这是开始所有这一切的原始问题:Auto layout issue with a row of image views

我接受了接受的答案,并且能够获得一个几乎正确的答案:

enter image description here

现在的问题是,由于某种原因,它正在打破约束,而且图像不再是完美的圆形。

我得到了5组破坏约束的警告。据我所知,它为每个图像视图打破了55的宽度。

我是自动布局的新手,我完全不知道为什么会这样。我将粘贴下面的输出,如果您需要更多信息,请告诉我。

第一

"<NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5470 UIImageView:0x7ffc2c035030.width == UIImageView:0x7ffc2c035030.height>",
"<NSLayoutConstraint:0x7ffc2c0f5570 V:[UIImageView:0x7ffc2c094a60(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5640 UIImageView:0x7ffc2c094a60.width == UIImageView:0x7ffc2c094a60.height>",
"<NSLayoutConstraint:0x7ffc2c0f5750 UIImageView:0x7ffc29deea00.width == UIImageView:0x7ffc29deea00.height>",
"<NSLayoutConstraint:0x7ffc2c0f57a0 V:[UIImageView:0x7ffc29deea00(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5860 V:[UIImageView:0x7ffc2c0e7b20(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f58b0 UIImageView:0x7ffc2c0e7b20.width == UIImageView:0x7ffc2c0e7b20.height>",
"<NSLayoutConstraint:0x7ffc2c0f5970 V:[UIImageView:0x7ffc2c0d7860(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f59c0 UIImageView:0x7ffc2c0d7860.width == UIImageView:0x7ffc2c0d7860.height>",
"<NSLayoutConstraint:0x7ffc2c0f60e0 UIImageView:0x7ffc2c035030.leading == UITableViewCellContentView:0x7ffc2c08ccc0.leadingMargin>",
"<NSLayoutConstraint:0x7ffc2c0f6130 H:[UIImageView:0x7ffc2c035030]-(8)-[UIImageView:0x7ffc2c094a60]>",
"<NSLayoutConstraint:0x7ffc2c0f6300 H:[UIImageView:0x7ffc2c094a60]-(8)-[UIImageView:0x7ffc29deea00]>",
"<NSLayoutConstraint:0x7ffc2c0f6460 H:[UIImageView:0x7ffc29deea00]-(8)-[UIImageView:0x7ffc2c0d7860]>",
"<NSLayoutConstraint:0x7ffc2c0f6630 H:[UIImageView:0x7ffc2c0d7860]-(8)-[UIImageView:0x7ffc2c0e7b20]>",
"<NSLayoutConstraint:0x7ffc2c0f6740 UITableViewCellContentView:0x7ffc2c08ccc0.trailingMargin == UIImageView:0x7ffc2c0e7b20.trailing - 1>",
"<NSLayoutConstraint:0x7ffc2c130dd0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7ffc2c08ccc0(375)]>"


Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7ffc2c0f5970 V:[UIImageView:0x7ffc2c0d7860(55)]>

第二

"<NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5470 UIImageView:0x7ffc2c035030.width == UIImageView:0x7ffc2c035030.height>",
"<NSLayoutConstraint:0x7ffc2c0f5570 V:[UIImageView:0x7ffc2c094a60(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5640 UIImageView:0x7ffc2c094a60.width == UIImageView:0x7ffc2c094a60.height>",
"<NSLayoutConstraint:0x7ffc2c0f5750 UIImageView:0x7ffc29deea00.width == UIImageView:0x7ffc29deea00.height>",
"<NSLayoutConstraint:0x7ffc2c0f57a0 V:[UIImageView:0x7ffc29deea00(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5860 V:[UIImageView:0x7ffc2c0e7b20(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f58b0 UIImageView:0x7ffc2c0e7b20.width == UIImageView:0x7ffc2c0e7b20.height>",
"<NSLayoutConstraint:0x7ffc2c0f60e0 UIImageView:0x7ffc2c035030.leading == UITableViewCellContentView:0x7ffc2c08ccc0.leadingMargin>",
"<NSLayoutConstraint:0x7ffc2c0f6130 H:[UIImageView:0x7ffc2c035030]-(8)-[UIImageView:0x7ffc2c094a60]>",
"<NSLayoutConstraint:0x7ffc2c0f6300 H:[UIImageView:0x7ffc2c094a60]-(8)-[UIImageView:0x7ffc29deea00]>",
"<NSLayoutConstraint:0x7ffc2c0f6460 H:[UIImageView:0x7ffc29deea00]-(8)-[UIImageView:0x7ffc2c0d7860]>",
"<NSLayoutConstraint:0x7ffc2c0f6630 H:[UIImageView:0x7ffc2c0d7860]-(8)-[UIImageView:0x7ffc2c0e7b20]>",
"<NSLayoutConstraint:0x7ffc2c0f6740 UITableViewCellContentView:0x7ffc2c08ccc0.trailingMargin == UIImageView:0x7ffc2c0e7b20.trailing - 1>",
"<NSLayoutConstraint:0x7ffc2c0f6570 UIImageView:0x7ffc2c0d7860.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c130dd0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7ffc2c08ccc0(375)]>"

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x7ffc2c0f5860 V:[UIImageView:0x7ffc2c0e7b20(55)]>

第三

"<NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5470 UIImageView:0x7ffc2c035030.width == UIImageView:0x7ffc2c035030.height>",
"<NSLayoutConstraint:0x7ffc2c0f5570 V:[UIImageView:0x7ffc2c094a60(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5640 UIImageView:0x7ffc2c094a60.width == UIImageView:0x7ffc2c094a60.height>",
"<NSLayoutConstraint:0x7ffc2c0f5750 UIImageView:0x7ffc29deea00.width == UIImageView:0x7ffc29deea00.height>",
"<NSLayoutConstraint:0x7ffc2c0f57a0 V:[UIImageView:0x7ffc29deea00(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f60e0 UIImageView:0x7ffc2c035030.leading == UITableViewCellContentView:0x7ffc2c08ccc0.leadingMargin>",
"<NSLayoutConstraint:0x7ffc2c0f6130 H:[UIImageView:0x7ffc2c035030]-(8)-[UIImageView:0x7ffc2c094a60]>",
"<NSLayoutConstraint:0x7ffc2c0f6300 H:[UIImageView:0x7ffc2c094a60]-(8)-[UIImageView:0x7ffc29deea00]>",
"<NSLayoutConstraint:0x7ffc2c0f6460 H:[UIImageView:0x7ffc29deea00]-(8)-[UIImageView:0x7ffc2c0d7860]>",
"<NSLayoutConstraint:0x7ffc2c0f6570 UIImageView:0x7ffc2c0d7860.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6630 H:[UIImageView:0x7ffc2c0d7860]-(8)-[UIImageView:0x7ffc2c0e7b20]>",
"<NSLayoutConstraint:0x7ffc2c0f6740 UITableViewCellContentView:0x7ffc2c08ccc0.trailingMargin == UIImageView:0x7ffc2c0e7b20.trailing - 1>",
"<NSLayoutConstraint:0x7ffc2c0f6790 UIImageView:0x7ffc2c0e7b20.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c130dd0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7ffc2c08ccc0(375)]>"

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7ffc2c0f57a0 V:[UIImageView:0x7ffc29deea00(55)]>

"<NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5470 UIImageView:0x7ffc2c035030.width == UIImageView:0x7ffc2c035030.height>",
"<NSLayoutConstraint:0x7ffc2c0f5570 V:[UIImageView:0x7ffc2c094a60(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5640 UIImageView:0x7ffc2c094a60.width == UIImageView:0x7ffc2c094a60.height>",
"<NSLayoutConstraint:0x7ffc2c0f60e0 UIImageView:0x7ffc2c035030.leading == UITableViewCellContentView:0x7ffc2c08ccc0.leadingMargin>",
"<NSLayoutConstraint:0x7ffc2c0f6130 H:[UIImageView:0x7ffc2c035030]-(8)-[UIImageView:0x7ffc2c094a60]>",
"<NSLayoutConstraint:0x7ffc2c0f6300 H:[UIImageView:0x7ffc2c094a60]-(8)-[UIImageView:0x7ffc29deea00]>",
"<NSLayoutConstraint:0x7ffc2c0f6460 H:[UIImageView:0x7ffc29deea00]-(8)-[UIImageView:0x7ffc2c0d7860]>",
"<NSLayoutConstraint:0x7ffc2c0f6570 UIImageView:0x7ffc2c0d7860.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6630 H:[UIImageView:0x7ffc2c0d7860]-(8)-[UIImageView:0x7ffc2c0e7b20]>",
"<NSLayoutConstraint:0x7ffc2c0f6740 UITableViewCellContentView:0x7ffc2c08ccc0.trailingMargin == UIImageView:0x7ffc2c0e7b20.trailing - 1>",
"<NSLayoutConstraint:0x7ffc2c0f6790 UIImageView:0x7ffc2c0e7b20.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6350 UIImageView:0x7ffc29deea00.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c130dd0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7ffc2c08ccc0(375)]>"

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x7ffc2c0f5570 V:[UIImageView:0x7ffc2c094a60(55)]>

第五

"<NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>",
"<NSLayoutConstraint:0x7ffc2c0f5470 UIImageView:0x7ffc2c035030.width == UIImageView:0x7ffc2c035030.height>",
"<NSLayoutConstraint:0x7ffc2c0f60e0 UIImageView:0x7ffc2c035030.leading == UITableViewCellContentView:0x7ffc2c08ccc0.leadingMargin>",
"<NSLayoutConstraint:0x7ffc2c0f6130 H:[UIImageView:0x7ffc2c035030]-(8)-[UIImageView:0x7ffc2c094a60]>",
"<NSLayoutConstraint:0x7ffc2c0f6300 H:[UIImageView:0x7ffc2c094a60]-(8)-[UIImageView:0x7ffc29deea00]>",
"<NSLayoutConstraint:0x7ffc2c0f6350 UIImageView:0x7ffc29deea00.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6460 H:[UIImageView:0x7ffc29deea00]-(8)-[UIImageView:0x7ffc2c0d7860]>",
"<NSLayoutConstraint:0x7ffc2c0f6570 UIImageView:0x7ffc2c0d7860.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6630 H:[UIImageView:0x7ffc2c0d7860]-(8)-[UIImageView:0x7ffc2c0e7b20]>",
"<NSLayoutConstraint:0x7ffc2c0f6740 UITableViewCellContentView:0x7ffc2c08ccc0.trailingMargin == UIImageView:0x7ffc2c0e7b20.trailing - 1>",
"<NSLayoutConstraint:0x7ffc2c0f6790 UIImageView:0x7ffc2c0e7b20.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c0f6240 UIImageView:0x7ffc2c094a60.width == UIImageView:0x7ffc2c035030.width>",
"<NSLayoutConstraint:0x7ffc2c130dd0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x7ffc2c08ccc0(375)]>"

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x7ffc2c0f5420 V:[UIImageView:0x7ffc2c035030(55)]>

据我所知,图片视图看起来不满意它们与下面的按钮和上面的标签对齐的方式。

由于

安迪

2 个答案:

答案 0 :(得分:1)

你过度约束你的观点。

你有一个约束,告诉你的图像视图有55高度和1.0宽高比,所以它们也有55宽度。 5 * 55用于图像加上4 * 8用于间距,为视图提供315的总宽度,但屏幕不是315宽,因此系统必须打破一些约束(它选择打破55在这种情况下,身高一。)

您需要删除55高度约束,以便图像视图的大小可以根据填充屏幕的宽度或更改间距约束而增大,以便它们可以增加图像视图之间的间距在保持相同尺寸的同时填充屏幕。

答案 1 :(得分:1)

所以我想出来了......

具有相等间距的堆栈视图和前导空间到边距使得间距约束根据需要随不同宽度而变化。为每个图像视图添加简单的宽高比和高度使其工作。