Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change constraints programmatically that is added from storyboard?

I have one screen. It will display like below

enter image description here

Now When User clicked I have an Account and Password(button) it will display like below

enter image description here

I want to move both views accordingly I added constraints using storyboard.Now need to change constraints from programming..

like image 274
Nilam Pari Avatar asked Nov 14 '16 07:11

Nilam Pari


People also ask

How do you add constraints to a storyboard?

Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

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

You need to create an IBOutlet of your constraint.
enter image description here

Then you set the constant value of your constraint in code:

labelWidthConstraint.constant = newValue 

If you want it animated you can do something like this:

Swift

labelWidthConstraint.constant = newValue UIView.animate(withDuration: 0.3, animations: {      self.view.layoutIfNeeded() }) 

Objective-C

self.labelWidthConstraint.constant = newValue; [UIView animateWithDuration:0.3 animations:^{             [self.view layoutIfNeeded]; }]; 
like image 94
Carien van Zyl Avatar answered Sep 27 '22 20:09

Carien van Zyl