约束问题

时间:2015-06-11 05:32:30

标签: ios objective-c autolayout nslayoutconstraint

我创建了UILabelUITextField,并以编程方式添加了constraint。以下是我在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)]];

运行时,textFieldlabel定位不正确,我收到以下错误:

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 个答案:

答案 0 :(得分:0)

尝试添加:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

我看到“NSAutoresizingMaskLayoutConstraint”,我想你不想要这些约束