iOS: Programmatically change height constraint of a UIView

Marcus picture Marcus · Feb 3, 2015 · Viewed 31.4k times · Source

I wonder is there a way to programmatically retrieve the height constraint of a specific UIView and change its value in the code? I have a situation where I have several UIViews with four edges pinned in the IB and height constraints added (IB complains if I did not), but in the middle of the program I would need to change the height of the UIViews and cause some UIViews to be pushed downward in the view.

I know I can ctrl+drag the height constraints of each UIViews and change their values in the code, but doing so would require me to wire dozens of them. So I think this is not efficient and not scalable in some way.

So I wonder is there a way to totally retrieve the height constraints of a specific view through code and change it dynamically?

Thanks!

Answer

Bhavin Chauhan picture Bhavin Chauhan · Feb 3, 2015

First add height constraint to your .h file:

//Add Outlet     
IBOutlet NSLayoutConstraint *ViewHeightConstraint;

Next, add the below code to your .m file:

//Set Value From Here   
ViewHeightConstraint.constant = 100;