【问题标题】:Issue with conflicting constraints约束冲突的问题
【发布时间】: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


【解决方案1】:

您可能会看到一条错误消息,内容如下:

无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

如果您看到该警告,则应将 translatesAutoresizingMaskIntoConstraints 设置为 false

selectBorderView.translatesAutoresizingMaskIntoConstraints = false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2018-03-29
    • 2021-08-04
    • 2013-02-16
    相关资源
    最近更新 更多