2016-11-17 100 views
3

那么,在代码中我添加了4个约束。但是当我在UI调试器中运行应用程序时,它显示了额外的6个约束?应该清楚这些4我补充说,我不知道我做错了什么?XCode添加重复约束?

let leftConstr = NSLayoutConstraint(item: image, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1.0, constant: 0.0) 
let bottomConstr = NSLayoutConstraint(item: image, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView , attribute: .bottom, multiplier: 1.0, constant: 0.0) 
let highthConstr = NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15) 
let widthConstr = NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50) 
cell.contentView.addConstraints([highthConstr, widthConstr, bottomConstr, leftConstr]) 

这里是我的UI调试器

https://i.stack.imgur.com/3fzc2.png

+1

我也看到过这种情况。如果我尝试以编程方式制作我的约束,它似乎会自动生成我没有创建的场景以使其工作。也许'翻译自动化大杂烩内容约束'就是这样做的。 – Sethmr

回答

4

也许你可以尝试之前添加这行代码的截图:

image.translatesAutoresizingMaskIntoConstraints = false 
+1

这是解决方案!谢谢@Matthias –

0

你两次建立约束上。一旦在代码中(请参阅您的问题)一次在故事板中(请参阅您的问题中的屏幕截图,imageView.bottom ...)。

决定什么适合您的需求,并删除其他。除非您必须动态更改约束,否则我会建议删除代码。

+2

这是我的UI调试器的截图,而不是UI生成器,它是在运行时创建的,我想在代码中设置我的约束。 :) –

+0

请在Storyboard中显示一个,并且还要显示imageView的大小检查器。 – shallowThought

+0

故事板中没有什么浅的,只是空的collectionView。我在名为cellForItemAt的UICollectionView委托中添加了这个UIImageView对象。 –