Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update height constraint programmatically

I am new in auto layout. I have done all of my project from xib file, but now I faced a problem where I have to update an view's height programmatically. I have tried below but now working.

[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:loginContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:loginFrame.size.height]]; 

In console it's shows

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:0x78724530 V:[UIView:0x790cdfb0(170)]>",     "<NSLayoutConstraint:0x787da210 V:[UIView:0x790cdfb0(400)]>" )  Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x78724530 V:[UIView:0x790cdfb0(170)]>  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. 
like image 969
Tapas Pal Avatar asked May 05 '15 18:05

Tapas Pal


People also ask

How do I change a constraint in Swift?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code. Method updateConstraints() is an instance method of UIView . It is helpful when you are setting the constraints programmatically.

How do I give a constraint to button programmatically in Swift?

Add the button in the view, give it constraints and as you are using constraints, you can skip the button. frame and add widthAnchor and heightAnchor . At last activate them and keep translatesAutoresizingMaskIntoConstraints as false . Also, it will be better if you can add proper names.

How do I change constraints in Xcode?

Clicking the Edit button in any of the constraints brings up a popover where you can change the constraint's relationship, constant, priority, or multiplier. To make additional changes, double-click the constraint to select it and open it in the Attribute inspector.


1 Answers

Instead of adding a new constraint, you need to modify the constant on your existing constraint.

Use an IBOutlet to connect to your constraint in Interface Builder:

@property (nonatomic, weak) NSLayoutConstraint *heightConstraint; 

Then, when you need to set it programmatically, simply set the constant property on the constraint:

heightConstraint.constant = 100; 

OR

If you can't access the nib in Interface Builder, find the constraint in code:

NSLayoutConstraint *heightConstraint; for (NSLayoutConstraint *constraint in myView.constraints) {     if (constraint.firstAttribute == NSLayoutAttributeHeight) {         heightConstraint = constraint;         break;     } } heightConstraint.constant = 100; 

And in Swift:

if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {             constraint.constant = 100.0         } 
like image 64
Dave Paul Avatar answered Sep 23 '22 07:09

Dave Paul