自动布局约束冲突

3
尝试创建一个包含UITextField和后面跟随的UILabel的视图。以下代码有什么问题?
- (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个回答

11

你忘记在 tf 上设置 translatesAutoresizingMaskIntoConstraintsNO。这个属性会防止布局引擎将旧的自动调整大小掩码自动转换为约束条件,因此出现了错误。


谢谢。SO说现在接受你的答案还为时过早,所以我会在10分钟后接受它。 - Shmidt
@Shmidt 没问题 :) - Jernej Strasner

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;即可。

我已经阅读了它,但是我将其应用于“视图”,但这对我没有产生任何影响。 - Shmidt
1
这行代码直接应用于UITextField "<NSAutoresizingMaskLayoutConstraint:0x15de2300 h=--& v=--& UITextField:0x15dbe780.midY ==>"。 - psci

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接