0
votes

I need to apply NSLayoutConstraints on a UIView with a UIImageView inside of it. I tried doing this programmatically, but I get errors that says unable to simultaneously satisfy constraints.

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
containerView.backgroundColor = [UIColor greenColor];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,50,50)];
imageView.backgroundColor = [UIColor redColor];

[self.view addSubview:containerView];
[containerView addSubview:imageView];

NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:
                       containerView,@"contrainerview",
                       imageView, @"myimageview",
                       nil];

containerView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(100)-[contrainerview(300)]->=0-|"] options:0  metrics:nil  views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[contrainerview(300)]->=0-|"] options:0  metrics:nil  views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(100)-[myimageview]->=0-|"] options:0  metrics:nil  views:views]];

2015-01-09 14:36:52.476 test[543:6554] 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) ( "", "" )

Will attempt to recover by breaking constraint

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.

2
please include the error(s) in the question - danh
I added the error. This line caused the error: [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(100)-[myimageview]->=0-|"] options:0 metrics:nil views:views]]; - iamarnold

2 Answers

0
votes

You can try this: Delete all translatesAutoresizingMaskIntoConstraints properties from xib file (Open xib as a source code).

0
votes

Since, you created everything dynamically, you also need to make imageView.translatesAutoresizingMaskIntoConstraints = NO;

NOTE: The views which are created programmatically translatesAutoresizingMaskIntoConstraints property of UIView must set to NO for properly working of constraints.