Issue with constraints

635 views Asked by At

I have a UILabel and a UITextField that I created, and added constraint programmatically. Here is the code I inserted in viewDidLoad:

UILabel *currencyLabel = [[UILabel alloc] init];
UITextField dollars = [[UITextField alloc] init];

CGRect label = CGRectMake(0, 0, 12, 33);
currencyLabel.frame = label;

CGRect textField = CGRectMake(12, 0, self.amountView.frame.size.width - 12, 33);
dollars.frame = textField;
[self.amountView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[currencyLabel(12)]-3-[dollars]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(currencyLabel, dollars)]];

[self.amountView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[currencyLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(currencyLabel)]];
[self.amountView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[dollars]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(dollars)]];

When I run it, the textField and label are not positioned correctly, and I get the following error:

2015-06-10 22:20:17.248 myApp[2591:134955] 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:0x7f82995382d0 H:|-(0)-[UILabel:0x7f829959e320]   (Names: '|':UIView:0x7f8299450070 )>",
    "<NSLayoutConstraint:0x7f82995bd6e0 H:[UILabel:0x7f829959e320(12)]>",
    "<NSLayoutConstraint:0x7f82995bd730 H:[UILabel:0x7f829959e320]-(3)-[UITextField:0x7f82994e8bf0]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f82995d4310 h=--& v=--& UITextField:0x7f82994e8bf0.midX == + 221>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f82995d4360 h=--& v=--& H:[UITextField:0x7f82994e8bf0(418)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f82995bd730 H:[UILabel:0x7f829959e320]-(3)-[UITextField:0x7f82994e8bf0]>

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-06-10 22:20:17.250 myApp[2591:134955] 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:0x7f82994edad0 H:[UIButton:0x7f829954ee80'Expense'(71)]>",
    "<NSLayoutConstraint:0x7f829953bec0 H:[UIButton:0x7f829954c6f0'Income'(71)]>",
    "<NSLayoutConstraint:0x7f82994503a0 UIButton:0x7f829954c6f0'Income'.leading == UITableViewCellContentView:0x7f82994582f0.leadingMargin>",
    "<NSLayoutConstraint:0x7f82994504b0 H:[UIButton:0x7f829954c6f0'Income']-(0)-[UIButton:0x7f829954ee80'Expense']>",
    "<NSLayoutConstraint:0x7f8299450550 UIView:0x7f8299450070.trailing == UITableViewCellContentView:0x7f82994582f0.trailingMargin>",
    "<NSLayoutConstraint:0x7f82994505a0 H:[UIButton:0x7f829954ee80'Expense']-(12)-[UIView:0x7f8299450070]>",
    "<NSLayoutConstraint:0x7f82995bd780 H:[UITextField:0x7f82994e8bf0]-(0)-|   (Names: '|':UIView:0x7f8299450070 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f82995d4310 h=--& v=--& UITextField:0x7f82994e8bf0.midX == + 221>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f82995d4360 h=--& v=--& H:[UITextField:0x7f82994e8bf0(418)]>",
    "<NSLayoutConstraint:0x7f82995c1280 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7f82994582f0(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f82995bd780 H:[UITextField:0x7f82994e8bf0]-(0)-|   (Names: '|':UIView:0x7f8299450070 )>

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.
1

There are 1 answers

1
Andrew Romanov On BEST ANSWER

Try add:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

I see "NSAutoresizingMaskLayoutConstraint", I think you don't want these constraints