Shmidt
Shmidt

Reputation: 16684

Autolayout constraints conflict

Trying to create a view with a UITextField and UILabel beyond it. What's wrong with the following code?

- (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];
}

Error message:

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.

Upvotes: 3

Views: 4128

Answers (2)

psci
psci

Reputation: 933

If you read error message cerfully you'll notice: (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

And later:

NSAutoresizingMaskLayoutConstraint:0x13a9eda0...

This means that you forgot to disable translates autoresizing mask.

Add tf.translatesAutoresizingMaskIntoConstraints = NO; to your code.

Upvotes: 1

Jernej Strasner
Jernej Strasner

Reputation: 4620

You forgot to set the translatesAutoresizingMaskIntoConstraints to NO on tf. This property prevents the layout engine to automatically transform the old style autoresizing masks into constraints, which is why the errors.

Upvotes: 12

Related Questions