【发布时间】:2016-02-29 14:59:16
【问题描述】:
我不确定我在这里遗漏了什么,我已经做了几个小时,查看了不断的教程,但我仍然遇到约束冲突的问题。基本上我有一个按钮,在这个按钮上我想添加一个新视图,它位于按钮的中心并且大小相同。当我去应用约束时,它告诉我存在冲突的约束,我只是不明白这里可能有什么冲突。
//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);
【问题讨论】:
-
您必须将
translatesAutoresizingMaskIntoConstraints设置为false。现在,您有两组相互冲突的约束,即您添加的约束和从调整大小蒙版创建的约束。 -
您提供的链接无效
-
它终于作为一个链接出现了,我不知道您是否进行了编辑,或者只是出现了故障
-
是的,就是这样做的,非常感谢,在上面找不到任何东西
-
如果您将其设为答案,我会将其标记为已接受
标签: swift autolayout nslayoutconstraint