2015-06-11 115 views
0

我有一個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. 
+0

什麼是amountView?它的UIView或視圖裏面的UIview –

+0

這是一個UIView .. – Eric

+0

如果它的主UIView沒有必要添加約束。刪除最後三行 –

回答

0

試加:

self.view.translatesAutoresizingMaskIntoConstraints = NO; 

我看到「NSAutoresizingMaskLayoutConstraint」,我想你不想要這些約束

+0

我必須設置標籤和textField的translateAutore ...到NO – Eric