Change AutoLayout Constraints during Run Time - ios

How is it possible to change AutoLayout Constraints during run time.
I need to change the coordinate of view during run time so i did following:
if ([OfferText length] != 0) {
cell.topContentView.translatesAutoresizingMaskIntoConstraints = YES;
CGRect newFrame = CGRectMake( 71, 4, [Util window_width]-71,50);
cell.topContentView.frame = newFrame;
}
While doing that I get following warning. Is it possible to change the auto layout constraints during run time.
(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:0x7fca729606a0 UIView:0x7fca7296c6e0.top == UITableViewCellContentView:0x7fca729712b0.topMargin + 2>",
"<NSAutoresizingMaskLayoutConstraint:0x7fca7286f620 h=--& v=--& UIView:0x7fca7296c6e0.midY == + 29>",
"<NSAutoresizingMaskLayoutConstraint:0x7fca7286f670 h=--& v=--& V:[UIView:0x7fca7296c6e0(50)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7fca705c9770 h=--& v=--& 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UITableViewCellContentView:0x7fca729712b0] (Names: '|':CollectionBrandCell:0x7fca72968820 )>"
)
I find my solution using following code:
in .h of tableviewcell In my case
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintsTopContentView;
and in .m
if ([OfferText length] != 0) {
cell.constraintsTopContentView.constant=-4;
}

If you are setting up constraints from Storyboard, then make the outlet's of the constraints that are required to change the view's co-ordinates.
For Example:
If you want to change the top space of some view XYZ, then please do the following:
Select that view's top constraint and make outlet in .h file. This will
look like this.
#property (strong, nonatomic) NSLayoutConstraint *XYZ'stopConstraint;
Now, change this constraints constant, when you required by the following
code.
self.XYZ'stopConstraints.constant = youValue;
Also, please remove this line from your code as it is not necessary as per my view.
cell.topContentView.translatesAutoresizingMaskIntoConstraints = YES;

Create outlets for constraints you want to change (similar to UI controls); then change constant property of them.
Your warning caused because you use cell.topContentView.translatesAutoresizingMaskIntoConstraints = YES;. This will creates some new constraints (NSAutoresizingMaskLayoutConstraint) and cause conflict with olds.

Related

Change Auto Constraint height of UIView

I have a UIView in a UITableViewCell. in the view, I have a UITextView and a UIView. They are both auto constraint on all 4 sides.
The UIView has a height constraint. I would like to update the height constraint prorrammatically. I made a outlet of the height constraint so I can change it.
Here is my code:
CGRect frame = self.myView.frame;
frame.size.height = 50;
frame.size.width = self.myView.frame.size.width;
self.myView.frame = frame;
self.viewHeight.constant = 50;
[myTableView reloadData];
When I run this code, it works perfectly, but I get an error: (I'm pretty new to objective c, so I don't understand what it's trying to say.)
2015-01-26 15:25:43.408 myApp[1449:55334] 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:0x7fb07853ac20 V:[UIView:0x7fb0784dbb80(50)]>",
"<NSLayoutConstraint:0x7fb0785bd170 V:|-(0)-[UITextView:0x7fb07896de00] (Names: '|':UIView:0x7fb07857fb70 )>",
"<NSLayoutConstraint:0x7fb0785bd260 V:[UITextView:0x7fb07896de00]-(3)-[UIView:0x7fb0784dbb80]>",
"<NSLayoutConstraint:0x7fb0785bd350 V:[UIView:0x7fb0784dbb80]-(0)-| (Names: '|':UIView:0x7fb07857fb70 )>",
"<NSLayoutConstraint:0x7fb0785bd5b0 UILabel:0x7fb0785150f0'info:'.top == UITableViewCellContentView:0x7fb078581400.topMargin>",
"<NSLayoutConstraint:0x7fb0785bd650 UITableViewCellContentView:0x7fb078581400.bottomMargin == UIView:0x7fb07857fb70.bottom + 1>",
"<NSLayoutConstraint:0x7fb0785bd6f0 V:[UILabel:0x7fb0785150f0'info:']-(8)-[UIView:0x7fb07857fb70]>",
"<NSLayoutConstraint:0x7fb078704f20 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fb078581400(77)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb07853ac20 V:[UIView:0x7fb0784dbb80(50)]>
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.
You programmaticlly change the height constraint to 50. It is unsatisfied with UIView-Encapsulated-Layout-Height constraint(77), which is set by iOS for UITableViewCellContentView.

How to change frame of uiview is using autolayout in iOS?

I added 1 view in XIB, it is set autolayout. But when run, i want to to change frame of that view. I know if using autolayout, need to set translatesAutoresizingMaskIntoConstraints = YES in my code. I have done that, I create new frame for that view but it shows the warning message in console:
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)
(
"<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e30 h=-&- v=-&- UIView:0x16ed3c30.midY == UIView:0x16ee06b0.midY + 44>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e60 h=-&- v=-&- UIView:0x16ed3c30.height == UIView:0x16ee06b0.height>"
)
Will attempt to recover by breaking constraint
<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>
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.
This is the code for create new frame, i just want the origin of Y is equal to 0.
self.leftView.frame= CGRectMake(0, 0, 40, self.view.frame.size.height);
How can i fix that bug? Please give some advice. Thanks much.
If I understand you correctly, I think you need to use a stored NSLayoutConstraint variable in your class, then update the constraint's constant value to 0 to make your UIView slide to the top.
Also, when you're using autolayout, you usually set:
myView.translateAutoResizingMaskIntoConstraint = NO;
That's NO instead of YES.
So an example would be something like:
#interface MyViewController()
{
NSLayoutConstraint *myViewTopConstraint;
}
#end
#implementation MyViewController
-(void)setupMyConstraintsMethod
{
...
// ---------------------------------------------------------------
// This constraint here says "myView" top edge should be pinned
// to the top edge of the view controller's view but with an offset
// of 50 pixels.
// ---------------------------------------------------------------
NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
[self.view addConstraint:myViewTopConstraint];
}
-(void)buttonPressed:(id)sender
{
// ------------------------------------------------------------
// now you want myView's top edge to be at the top of your
// view controller's view, so you set the constant to 0
// ------------------------------------------------------------
myViewTopConstraint.constant = 0;
// animates the sliding up of "myView"
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
#end

Debug constraints when setting view programmatically

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.

Why aren't these constraints working?

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.

Autolayout constraint conflict when I specified just one constraint

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)

Resources