簡體   English   中英

約束沖突的問題

[英]Issue with conflicting constraints

我不確定我在這里缺少什么,我已經花了幾個小時,看了不斷的教程,但是仍然遇到約束沖突的問題。 基本上,我有一個按鈕,在此按鈕上,我想添加一個新的視圖,該視圖以按鈕為中心且大小相同。 當我去應用約束時,它告訴我存在沖突的約束,而我只是不了解這里可能發生什么沖突。

//Create a new view
let selectBorderView = UIView();
//Guarantee there are no constraints attached to it
NSLayoutConstraint.deactivateConstraints(selectBorderView.constraints);
//Add the new view to the button
sender.addSubview(selectBorderView);

//Create constraint selectBorderView.width = button.width        
let constraintEW = NSLayoutConstraint(item: selectBorderView, attribute: .Width, relatedBy: .Equal, toItem: selectBorderView.superview, attribute: .Width, multiplier: 1, constant: 0);
//Create constraint selectBorderView.height = button.height        
let constraintEH = NSLayoutConstraint(item: selectBorderView, attribute: .Height, relatedBy: .Equal, toItem: selectBorderView.superview, attribute: .Height, multiplier: 1, constant: 0);
//Create constraint selectBorderView.centerX = button.centerX        
let constraintCX = NSLayoutConstraint(item: selectBorderView, attribute: .CenterX, relatedBy: .Equal, toItem: selectBorderView.superview!, attribute: .CenterX, multiplier: 1, constant: 0);
//Create constraint selectBorderView.centerY = button.centerY
let constraintCY = NSLayoutConstraint(item: selectBorderView, attribute: .CenterY, relatedBy: .Equal, toItem: selectBorderView.superview, attribute: .CenterY, multiplier: 1, constant: 0);

//add the constraints to the button        
selectBorderView.superview!.addConstraint(constraintEW);
selectBorderView.superview!.addConstraint(constraintEH);
selectBorderView.superview!.addConstraint(constraintCX);
selectBorderView.superview!.addConstraint(constraintCY);

您可能會看到一條錯誤消息,內容如下:

無法同時滿足約束條件。 以下列表中至少有一個約束是您不想要的約束。 嘗試以下操作:(1)查看每個約束,並嘗試找出不期望的約束; (2)查找添加了一個或多個不必要約束的代碼並進行修復。 (注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,請參閱有關UIView屬性translationsAutoresizingMaskIntoConstraints的文檔)

如果看到該警告,則應將translatesAutoresizingMaskIntoConstraints設置為false

selectBorderView.translatesAutoresizingMaskIntoConstraints = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM