Swift约束错误(程序性约束)

时间:2015-02-26 07:43:54

标签: ios swift xcode6 nslayoutconstraint

我现在一直在尝试为2个简单标签添加约束。我可以让其中一个工作没有任何问题,但第二个我尝试添加约束到我的其他标签xcode提供以下错误:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17008faa0 H:[UILabel:0x147519bd0'WINNING'(382)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x170090950 h=--& v=--& H:[UILabel:0x147519bd0'WINNING'(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17008faa0 H:[UILabel:0x147519bd0'WINNING'(382)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-02-26 02:33:29.972 Formula Stocks[1804:220244] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17008faf0 V:[UILabel:0x147519bd0'WINNING'(110)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700909f0 h=--& v=--& V:[UILabel:0x147519bd0'WINNING'(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17008faf0 V:[UILabel:0x147519bd0'WINNING'(110)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-02-26 02:33:29.974 Formula Stocks[1804:220244] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17008fbe0 H:|-(16)-[UILabel:0x14760ab80'BE ON THE']   (Names: '|':UIView:0x170197690 )>",
    "<NSLayoutConstraint:0x17008fd20 UILabel:0x14760ab80'BE ON THE'.leading == UILabel:0x147519bd0'WINNING'.leading>",
    "<NSAutoresizingMaskLayoutConstraint:0x170090900 h=--& v=--& UILabel:0x147519bd0'WINNING'.midX ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x170090950 h=--& v=--& H:[UILabel:0x147519bd0'WINNING'(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408cda0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x170197690]   (Names: '|':UIViewControllerWrapperView:0x170196c00 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17008fd20 UILabel:0x14760ab80'BE ON THE'.leading == UILabel:0x147519bd0'WINNING'.leading>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-02-26 02:33:29.975 Formula Stocks[1804:220244] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17008f5a0 V:[UILabel:0x14760ab80'BE ON THE'(110)]>",
    "<NSLayoutConstraint:0x17008fc80 V:|-(8)-[UILabel:0x14760ab80'BE ON THE']   (Names: '|':UIView:0x170197690 )>",
    "<NSLayoutConstraint:0x17008fcd0 V:[UILabel:0x14760ab80'BE ON THE']-(NSSpace(8))-[UILabel:0x147519bd0'WINNING']>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700909a0 h=--& v=--& UILabel:0x147519bd0'WINNING'.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700909f0 h=--& v=--& V:[UILabel:0x147519bd0'WINNING'(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408cd00 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x170197690]   (Names: '|':UIViewControllerWrapperView:0x170196c00 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17008f5a0 V:[UILabel:0x14760ab80'BE ON THE'(110)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-02-26 02:33:29.977 Formula Stocks[1804:220244] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17008fc80 V:|-(8)-[UILabel:0x14760ab80'BE ON THE']   (Names: '|':UIView:0x170197690 )>",
    "<NSLayoutConstraint:0x17008fcd0 V:[UILabel:0x14760ab80'BE ON THE']-(NSSpace(8))-[UILabel:0x147519bd0'WINNING']>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700909a0 h=--& v=--& UILabel:0x147519bd0'WINNING'.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1700909f0 h=--& v=--& V:[UILabel:0x147519bd0'WINNING'(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17408cd00 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x170197690]   (Names: '|':UIViewControllerWrapperView:0x170196c00 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17008fcd0 V:[UILabel:0x14760ab80'BE ON THE']-(NSSpace(8))-[UILabel:0x147519bd0'WINNING']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
除此之外,我的第二个标签甚至都没有显示出来。

这里是代码:

var title1 = UILabel()
title1.setTranslatesAutoresizingMaskIntoConstraints(false)
title1.text = "BE ON THE"
title1.font = UIFont(name: "BebasNeueBold", size: 124)
title1.backgroundColor = UIColor.greenColor()
//title1.textAlignment = .Center
self.view.addSubview(title1)

var title2 = UILabel()
title1.setTranslatesAutoresizingMaskIntoConstraints(false)
title2.text = "WINNING"
title2.font = UIFont(name: "BebasNeueBold", size: 124)
title2.textColor = formulaWhiteColor
title2.backgroundColor = UIColor.orangeColor()
self.view.addSubview(title2)

//--------------- constraints ---------------//

//make dictionary for views
let viewsDictionary = ["title1":title1,"title2":title2]
let metricsDictionary = ["titleWidth":view.frame.width-32,"title1Height":110,"title2Height":150]


//sizing constraints

//title1
let title1_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[title1(titleWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let title1_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[title1(title1Height)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

title1.addConstraints(title1_constraint_H)
title1.addConstraints(title1_constraint_V)


//view2
let title2_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[title2(titleWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let title2_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:[title2(title1Height)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

title2.addConstraints(title2_constraint_H)
title2.addConstraints(title2_constraint_V)

//position constraints

//views
let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-16-[title1]-16-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
//let view_constraint_H2:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-16-[title2]-16-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[title1]-[title2]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)

view.addConstraints(view_constraint_H)
//view.addConstraints(view_constraint_H2)
view.addConstraints(view_constraint_V)

任何人都可以解释我哪里出错了吗?

我试着评论出所有的title2约束,然后逐个测试它们。所有这些,单独或一起导致错误。

我还应该提一下,第二个约束被添加到title2,我不能让它显示在屏幕的任何地方。

1 个答案:

答案 0 :(得分:0)

我仍然不知道到底哪里出错了。但我删除了所有约束并从头开始。不知怎的,它现​​在工作正常。 :/