Autolayout约束冲突

时间:2014-07-14 10:07:02

标签: ios objective-c nsautolayout

尝试创建一个超出它的UITextFieldUILabel的视图。 以下代码有什么问题?

- (UIView *)tableHeaderView{
    NSArray *constraints;
    UIView *view = [[UIView alloc]initWithFrame:(CGRect){0,0,self.view.frame.size.width, 88}];

    UITextField *tf = [[UITextField alloc]init];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    tf.text = _filter.name;
    [view addSubview:tf];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Found results: %d", nil), _filter.resultsCount];
    [view addSubview:titleLabel];

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[titleLabel]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)];
    [view addConstraints:constraints];

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[tf]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tf)];
    [view addConstraints:constraints];

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleLabel]-8-[tf]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(titleLabel, tf)];
    [view addConstraints:constraints];
}

错误讯息:

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:0x15dd2fc0 V:|-(0)-[UILabel:0x15dd1e10]   (Names: '|':UIView:0x15dc4f90 )>",
    "<NSLayoutConstraint:0x15dd3120 V:[UILabel:0x15dd1e10]-(8)-[UITextField:0x15dbe780]>",
    "<NSAutoresizingMaskLayoutConstraint:0x15de2300 h=--& v=--& UITextField:0x15dbe780.midY ==>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15dd3120 V:[UILabel:0x15dd1e10]-(8)-[UITextField:0x15dbe780]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-07-14 14:01:30.216 DossierPolice[4724:60b] 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:0x15dd2e20 H:[UITextField:0x15dbe780]-(NSSpace(20))-|   (Names: '|':UIView:0x15dc4f90 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x15de1ee0 h=--& v=--& UITextField:0x15dbe780.midX ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x15de22d0 h=--& v=--& H:[UITextField:0x15dbe780(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x13a9eda0 h=--& v=--& H:[UIView:0x15dc4f90(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15dd2e20 H:[UITextField:0x15dbe780]-(NSSpace(20))-|   (Names: '|':UIView:0x15dc4f90 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-07-14 14:01:30.217 DossierPolice[4724:60b] 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:0x15dd3160 V:[UITextField:0x15dbe780]-(0)-|   (Names: '|':UIView:0x15dc4f90 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x15de2300 h=--& v=--& UITextField:0x15dbe780.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x15de2330 h=--& v=--& V:[UITextField:0x15dbe780(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x13a9ee00 h=--& v=--& V:[UIView:0x15dc4f90(88)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15dd3160 V:[UITextField:0x15dbe780]-(0)-|   (Names: '|':UIView:0x15dc4f90 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

2 个答案:

答案 0 :(得分:10)

您忘了在translatesAutoresizingMaskIntoConstraints上将NO设置为tf。此属性可防止布局引擎自动将旧样式自动调整掩码转换为约束,这就是错误的原因。

答案 1 :(得分:1)

如果您错误地阅读了错误消息,您会注意到:(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

后来:

  

NSAutoresizingMaskLayoutConstraint:0x13a9eda0 ...

这意味着您忘记禁用翻译自动调整遮罩。

tf.translatesAutoresizingMaskIntoConstraints = NO;添加到您的代码中。