2016-11-17 98 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對象。 –