I am programatically setting the following constraints on three views inside a UIView:
UIView *view1 = ((UIViewController *)[self.viewControllers objectAtIndex:0]).view;
UIView *view2 = ((UIViewController *)[self.viewControllers objectAtIndex:1]).view;
UIView *view3 = ((UIViewController *)[self.viewControllers objectAtIndex:2]).view;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view1, view2, view3);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[view1(==320)]-0-[view2(==320)]-0-[view3(==320)]|" options:0 metrics:0 views:viewsDictionary]];
My intention is that each view is 320px wide, sitting flush against one-another with no gap, and view one up against the left edge of the containing UIView.
However, when compiled, the views are laid out on-top of one-another and I get the following in the console:
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:0x211811c0 H:|-(0)-[UIView:0xc683790] (Names: '|':UIView:0xc683320 )>",
"<NSAutoresizingMaskLayoutConstraint:0x21179820 h=-&- v=-&- UIView:0xc683790.midX == UIView:0xc683320.midX - 340>",
"<NSLayoutConstraint:0x211793d0 H:[UIView:0xc6a50c0]-(0)-| (Names: '|':UIView:0xc683320 )>",
"<NSLayoutConstraint:0x21179390 H:[UIView:0xc6a50c0(320)]>",
"<NSLayoutConstraint:0x21179350 H:[UIView:0xc6a05f0]-(0)-[UIView:0xc6a50c0]>",
"<NSLayoutConstraint:0x211563d0 H:[UIView:0xc6a05f0(320)]>",
"<NSLayoutConstraint:0x21156390 H:[UIView:0xc683790]-(0)-[UIView:0xc6a05f0]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x21179350 H:[UIView:0xc6a05f0]-(0)-[UIView:0xc6a50c0]>
Try setting
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
for all of your views, it usually is enough to solve this kind of problem.
Related
I'm practicing with visual format layout using Objective-C & XCode 7 but struggling with getting a very simple layout to work.
My app is just a simple Single View application and I'm trying to add a UILabel to main view.
Below is the code.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILabel *label = [[UILabel alloc] init];
label.text = #"Hello world";
label.layer.borderWidth = 1.0f;
label.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:label];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(label);
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"|[label]|"
options:0
metrics:nil views:viewsDictionary]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[label]|"
options:0
metrics:nil views:viewsDictionary]];
NSLog(#"constraints: %#", self.view.constraints);
}
Run this I got a bunch of errors:
2016-02-01 18:14:15.729 AutolayoutVisualFormat[26671:909266] constraints: (
"<_UILayoutSupportConstraint:0x79771ab0 V:[_UILayoutGuide:0x7976f950(0)]>",
"<_UILayoutSupportConstraint:0x79786d30 V:|-(0)-[_UILayoutGuide:0x7976f950] (Names: '|':UIView:0x79771790 )>",
"<_UILayoutSupportConstraint:0x797884e0 V:[_UILayoutGuide:0x79772e30(0)]>",
"<_UILayoutSupportConstraint:0x79777540 _UILayoutGuide:0x79772e30.bottom == UIView:0x79771790.bottom>",
"<NSLayoutConstraint:0x7978d930 H:|-(0)-[UILabel:0x7976d000'Hello world'] (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7978da50 H:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7978fcb0 V:|-(0)-[UILabel:0x7976d000'Hello world'] (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7978fce0 V:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>"
)
2016-02-01 18:14:15.733 AutolayoutVisualFormat[26671:909266] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x7a0630a0 h=--& v=--& H:[UILabel:0x7976d000'Hello world'(0)]>",
"<NSLayoutConstraint:0x7978d930 H:|-(0)-[UILabel:0x7976d000'Hello world'] (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7978da50 H:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7a080130 'UIView-Encapsulated-Layout-Width' H:[UIView:0x79771790(320)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7978da50 H:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>
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.
2016-02-01 18:14:15.733 AutolayoutVisualFormat[26671:909266] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x7a0758b0 h=--& v=--& V:[UILabel:0x7976d000'Hello world'(0)]>",
"<NSLayoutConstraint:0x7978fcb0 V:|-(0)-[UILabel:0x7976d000'Hello world'] (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7978fce0 V:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>",
"<NSLayoutConstraint:0x7a081610 'UIView-Encapsulated-Layout-Height' V:[UIView:0x79771790(568)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7978fce0 V:[UILabel:0x7976d000'Hello world']-(0)-| (Names: '|':UIView:0x79771790 )>
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.
Add this line it will work
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
When using Auto Layout on views created in code, there are a few
caveats to be aware of. The first is related to the value of the
property translatesAutoresizingMaskIntoConstraints. This property is
YES by default, which means Auto Layout constraints will be created
based on the view's autoresizing mask. We want the view to respect
the Auto Layout constraints we will add so this property should be
set to NO.
It was old technology that was earlier used for managing view sizes
You can refer this link UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C
When this property is YES, which it is by default, the autoresizing mask of a view is translated into constraints. For example, if a view
is configured as in Figure 6-1 and
translatesAutoresizingMaskIntoConstraints is YES, then the
constraints |-20-[button]-20-| and V:|-20-[button(20)] are added to
the view’s superview. The net effect is that unaware views behave as
they did in versions of OS X prior to 10.7.
For views that are aware of Auto Layout, in most circumstances you will want translatesAutoresizingMaskIntoConstraints to be NO.
This is because the constraints generated by translating the
autoresizing mask are already sufficient to completely specify the
frame of a view given its superview’s frame, which is generally too
much. For example, this will prevent a button from automatically
assuming its optimal width when its title is changed.
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.
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.
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.
I have a complex set of labels and buttons. I set everything programmatically using mainly visual format language. The UILabels are created with the following method:
-(UILabel*)createLabelWithPlaceHolder:(NSString*)placeHolder{
UILabel *provisionalLabel=[[UILabel alloc] init];
provisionalLabel =[[UILabel alloc] init];
provisionalLabel.translatesAutoresizingMaskIntoConstraints=NO;
provisionalLabel.text=placeHolder;
provisionalLabel.font=[UIFont systemFontOfSize:12.0f];
provisionalLabel.backgroundColor=[UIColor greenColor];
provisionalLabel.textAlignment=NSTextAlignmentCenter;
[provisionalLabel setContentHuggingPriority:200 forAxis:UILayoutConstraintAxisHorizontal];
[provisionalLabel setContentCompressionResistancePriority:900 forAxis:UILayoutConstraintAxisHorizontal];
return provisionalLabel;
}
and the labels are displayed according to the following VLF code:
gap=(self.vistaGris.bounds.size.width*secondRowFactor)/7;
labelWidth=(self.vistaGris.bounds.size.width*(1-secondRowFactor))/6;
gapN=[NSNumber numberWithFloat:gap];
labelWidthN=[NSNumber numberWithFloat:labelWidth];
constraints = [NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-gap-[solution4(width)]-gap-[solution5(width)]-gap-[solution6(width)]-gap-[solution7(width)]-gap-[solution8(width)]-gap-[solution9(width)]-gap-|"
options:NSLayoutFormatAlignAllBaseline
metrics:#{#"gap":gapN, #"width":labelWidthN}
views:viewsDictionary];
[self.vistaGris addConstraints:constraints];
Basically what it does is to display 6 labels on the bottom of the view. With equal spaces between them and same width. The device orientation is always landscape all the time.
The code does what I want it to do and everything is displayed OK. However I got a console warning telling me that the system cannot satisfy simultaneous constraints:
2014-04-19 09:59:48.749 Concentrations[431: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:0x8d6cd70 Linees:0x8c82d70.width == UIView:0x8c41c40.width>",
"<NSLayoutConstraint:0x8d84480 H:|-(6.85714)-[UILabel:0x8d65460] (Names: '|':Linees:0x8c82d70 )>",
"<NSLayoutConstraint:0x8d844d0 H:[UILabel:0x8d65460(72)]>",
"<NSLayoutConstraint:0x8d84500 H:[UILabel:0x8d65460]-(6.85714)-[UILabel:0x8d73890]>",
"<NSLayoutConstraint:0x8d84550 H:[UILabel:0x8d73890(72)]>",
"<NSLayoutConstraint:0x8d845b0 H:[UILabel:0x8d73890]-(6.85714)-[UILabel:0x8d731b0]>",
"<NSLayoutConstraint:0x8d845e0 H:[UILabel:0x8d731b0(72)]>",
"<NSLayoutConstraint:0x8d84640 H:[UILabel:0x8d731b0]-(6.85714)-[UILabel:0x8d734e0]>",
"<NSLayoutConstraint:0x8d84690 H:[UILabel:0x8d734e0(72)]>",
"<NSLayoutConstraint:0x8d84700 H:[UILabel:0x8d734e0]-(6.85714)-[UILabel:0x8d741b0]>",
"<NSLayoutConstraint:0x8d84730 H:[UILabel:0x8d741b0(72)]>",
"<NSLayoutConstraint:0x8d847d0 H:[UILabel:0x8d741b0]-(6.85714)-[UILabel:0x8d744d0]>",
"<NSLayoutConstraint:0x8d84800 H:[UILabel:0x8d744d0(72)]>",
"<NSLayoutConstraint:0x8d84860 H:[UILabel:0x8d744d0]-(6.85714)-| (Names: '|':Linees:0x8c82d70 )>",
"<NSAutoresizingMaskLayoutConstraint:0x8c8aef0 h=--& v=--& V:[UIView:0x8c41c40(480)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x8d847d0 H:[UILabel:0x8d741b0]-(6.85714)-[UILabel:0x8d744d0]>
The thing is that I don't set constraints for these labels anywhere else on my code and this debugger warning could not be less explanatory.
The warning says that you have an NSAutoresizingMaskLayoutConstraint (it derives from the autoresize automatically translated to constraints). Probably you want to set translatesAutoresizingMaskIntoConstraints=NO not only on the labels but on the vistaGris too.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ChildViewController *childviewcontroller = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
[self addChildViewController:childviewcontroller];
[self.view addSubview:childviewcontroller.view];
[childviewcontroller willMoveToParentViewController:self];
UIView *cview = [[UIView alloc] init];
cview = childviewcontroller.view;
[self.view removeConstraints:self.view.constraints];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(cview);
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|-[cview]-|"
options:0 metrics:nil
views:viewsDictionary]];
}
I want to add childviewcontroller view over parent view. After adding I set the constraint but it is not working for me.
I am also getting warnings like this
2013-07-25 10:47:30.564 neenah[1105:c07] 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:0x9256c90 H:|-(NSSpace(20))-[UIView:0x9256a00] (Names: '|':UIView:0x92557a0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x755d690 h=--& v=--& H:[UIView:0x9256a00(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x9256c90 H:|-(NSSpace(20))-[UIView:0x9256a00] (Names: '|':UIView:0x92557a0 )>
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.
2013-07-25 10:47:30.567 neenah[1105:c07] 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:0x9256bb0 H:[UIView:0x9256a00]-(NSSpace(20))-| (Names: '|':UIView:0x92557a0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7561690 h=--- v=--- H:[UIWindow:0x92527c0(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0x755fe50 h=-&- v=-&- UIView:0x92557a0.width == UIWindow:0x92527c0.width>",
"<NSAutoresizingMaskLayoutConstraint:0x755d690 h=--& v=--& H:[UIView:0x9256a00(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x9256bb0 H:[UIView:0x9256a00]-(NSSpace(20))-| (Names: '|':UIView:0x92557a0 )>
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.
A few observations:
You should turn off translatesAutoresizingMaskIntoConstraints:
childviewcontroller.view.translatesAutoresizingMaskIntoConstraints = NO;
You should define vertical constraints, too:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[cview]-|"
options:0
metrics:nil
views:viewsDictionary]];
Unrelated to your problem, you don't need to create the [[UIView alloc] init] for cview. You're immediately discarding it.
I'm not sure why you're removing the constraints for self.view. (I'm presuming you did that as you were tearing your hair out in your testing.) You don't have to do that. But if you have something else going on here that made you think you needed to do that, let us know what that is.
When adding a child controller, you call didMoveToParentViewController, not willMoveToParentViewController. The addChildViewController calls willMoveToParentViewController for you. You only need the didMove... rendition.
Thus:
- (void)viewDidLoad {
[super viewDidLoad];
// instantiate the view controller
ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
// or you can instantiate using storyboard
//
// ChildViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildIdentifier"];
// now do the view controller containment calls to update the view controller hierarchy and add the view as appropriate
[self addChildViewController:childViewController];
childViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
UIView *childView = childViewController.view;
NSDictionary *views = NSDictionaryOfVariableBindings(childView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[childView]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[childView]-|" options:0 metrics:nil views:views]];
}
I have the following simple test code in my controller:
- (void)loadView
{
UIView *view = [UIView new];
[self setView:view];
UILabel *label = [UILabel new];
[label setText:#"Hello World!"];
[view addSubview:label];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|-[label]"
options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
}
The code fails with the following exception and I can't figure out why. Any help would be greatly appreciated:
2013-04-15 14:15:47.880 libmarkup-test[1072:c07] 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:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60] (Names: '|':UIView:0x75376a0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x712a2c0 h=--& v=--& UILabel:0x7536b60.midX ==>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60] (Names: '|':UIView:0x75376a0 )>
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.
The "unable to simultaneously satisfy constraints" message is especially confusing since, as far as I can tell, I'm only specifying one constraint.
It looks like you forgot to set translatesAutoresizingMaskIntoConstraints on the UILabel. By default it will be YES. So the autoresizing mask on that label are translated to additional constraints, which are then conflicting with the one you specified.
Adding this should fix the constraint issue:
label.translatesAutoresizingMaskIntoConstraints = NO;
You should probably think about a vertical constraints on that label as well.
please adjust the constraint priority( default is 1000) and hugging priority(default is 250)