Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift constraints error? (programmatic constraints)

I'm having some trouble with my programatic constraints. I'm still in a very simple phase, so it shouldn't get much easier than this.

I have 2 lables named "title1" and "title2", I am able to place title1, 8 points from the top, 16 points from the left, 16 points from the right, make a height and width constraint.

However, the second I try to add constraints to "title2" xcode throws this error at me:


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:0x170085be0 H:[UILabel:0x125d12480'WINNING'(50)]>", "<NSAutoresizingMaskLayoutConstraint:0x170086a40 h=--& v=--& H:[UILabel:0x125d12480'WINNING'(0)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x170085be0 H:[UILabel:0x125d12480'WINNING'(50)]>

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:08:53.305 Formula Stocks[1777:214787] 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:0x170085c80 H:|-(16)-[UILabel:0x125e13ed0'BE ON THE'] (Names: '|':UIView:0x1741998b0 )>", "<NSLayoutConstraint:0x170085f00 UILabel:0x125e13ed0'BE ON THE'.leading == UILabel:0x125d12480'WINNING'.leading>", "<NSAutoresizingMaskLayoutConstraint:0x1700869f0 h=--& v=--& UILabel:0x125d12480'WINNING'.midX ==>", "<NSAutoresizingMaskLayoutConstraint:0x170086a40 h=--& v=--& H:[UILabel:0x125d12480'WINNING'(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x174085dc0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x1741998b0] (Names: '|':UIViewControllerWrapperView:0x1701998b0 )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x170085f00 UILabel:0x125e13ed0'BE ON THE'.leading == UILabel:0x125d12480'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:08:53.307 Formula Stocks[1777:214787] 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:0x170085910 V:[UILabel:0x125e13ed0'BE ON THE'(110)]>", "<NSLayoutConstraint:0x170085d70 V:|-(8)-[UILabel:0x125e13ed0'BE ON THE'] (Names: '|':UIView:0x1741998b0 )>", "<NSLayoutConstraint:0x170085eb0 V:[UILabel:0x125e13ed0'BE ON THE']-(NSSpace(8))-[UILabel:0x125d12480'WINNING']>", "<NSAutoresizingMaskLayoutConstraint:0x170086a90 h=--& v=--& UILabel:0x125d12480'WINNING'.midY ==>", "<NSAutoresizingMaskLayoutConstraint:0x170086ae0 h=--& v=--& V:[UILabel:0x125d12480'WINNING'(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x174085d20 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x1741998b0] (Names: '|':UIViewControllerWrapperView:0x1701998b0 )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x170085910 V:[UILabel:0x125e13ed0'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:08:53.308 Formula Stocks[1777:214787] 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:0x170085d70 V:|-(8)-[UILabel:0x125e13ed0'BE ON THE'] (Names: '|':UIView:0x1741998b0 )>", "<NSLayoutConstraint:0x170085eb0 V:[UILabel:0x125e13ed0'BE ON THE']-(NSSpace(8))-[UILabel:0x125d12480'WINNING']>", "<NSAutoresizingMaskLayoutConstraint:0x170086a90 h=--& v=--& UILabel:0x125d12480'WINNING'.midY ==>", "<NSAutoresizingMaskLayoutConstraint:0x170086ae0 h=--& v=--& V:[UILabel:0x125d12480'WINNING'(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x174085d20 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x1741998b0] (Names: '|':UIViewControllerWrapperView:0x1701998b0 )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x170085eb0 V:[UILabel:0x125e13ed0'BE ON THE']-(NSSpace(8))-[UILabel:0x125d12480'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.


beyond that, my second label doesn't even show up.

here's the broken code:

    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_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[title1]-[title2]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)

    view.addConstraints(view_constraint_H)
    view.addConstraints(view_constraint_V)

Can anyone explain where I am going wrong?

I tried commenting out all the title2 constraints, and test them one by one. All of them, alone or together causes the error.

I should also mention, that the second constraints are added to title2, I can't get it to show up anywhere on the screen.

I'm actually having the same issue, but mine is even more simple....

I've got a background image which i've set contraints to 0, 0, 0, 0 and that all works fine however when i now add a label and fix my contraints for it to say - 277 left, 277 right, 43 bottom i get that error messsage you've got...... i.e.

2015-04-28 16:08:49.018 Photog[8400:415659] 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) ( "", "", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Have you managed to find a fix for this, or has anyone else seen this problem?

1 Answer

Hey Mark Lyck ,

I've been painfully working for 2 days straight trying to figure out why this is happening, but i still haven't figured out the exact reason why mine was producing this error in the logs (seems that maybe the constraints are too ambiguous, what ever that means :) ) But i did find a fix to this issue (for me at least), once your set your constraint for that particular element, and you run the code/app and it doesn't display or throws that error again, try choosing "reset to suggested constraints" under the Resolve Auto-Layout Issues for that particular element.

Hope that helps, and maybe someone else might have a more definitive answer as to why this happens, in the future.

Gareth