iOS: Programmatically change height constraint of a UIView
Asked Answered
H

2

33

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!

Hedwig answered 3/2, 2015 at 7:54 Comment(0)
L
41

You can connect that specific constraint to your code from the interface builder like

enter image description here

And NSLayoutConstraint has a constant property to be set which is your constant pin value.

If you're adding your constraints programmatically, you can surely benefit from the identifier property in NSLayoutConstraint class. Set them and iterate over them to get that specific identifired constraint.

As the documentation says

extension NSLayoutConstraint {
    /* For ease in debugging, name a constraint by setting its identifier, which will be printed in the constraint's description.
     Identifiers starting with UI and NS are reserved by the system.
     */
    @availability(iOS, introduced=7.0)
    var identifier: String?
}
Ligure answered 3/2, 2015 at 7:58 Comment(5)
Thanks for the quick answer. I'm aware of this method but I just wonder can it be done programmatically? Cause I have a huge number of views and subviews so to wire them up one by one is going to make the whole thing ends up looking super messy and hard to maintain.Hedwig
If you're adding your constraints programmatically, you can surely benefit from the identifier property in NSLayoutConstraint class. Set them and iterate over them to get that specific identifired constraint.Ligure
Wow..This could be the one I'm looking for. Let me try when I get back to my mac. Thanks a lot for the help!Hedwig
this would be the same for ios swift, right? just making sureLilililia
@Lilililia yes it would.Ligure
O
42

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;
Ortensia answered 3/2, 2015 at 8:52 Comment(4)
I have done exactly same but inside the view there are some texts and labels which have there height constraints. when i set wrapperViewHeight to zero its subviews are not hiding i want them to hide along with the view and other views below it would adjust towards upside automatically.Jocular
@Jocular You can hide the views inside the containerview. and on setting the height constraint of wrapperview to zero shift the bottomviews to upward.Ortensia
Set the drawing property "Clip Subviews" of your view in storyboard/xib on. This will cut the parts outside the frame of the view and you will get the wanted effect.Vespertilionine
I don't have a xib file, how can I get the outlet in code.Lenity
L
41

You can connect that specific constraint to your code from the interface builder like

enter image description here

And NSLayoutConstraint has a constant property to be set which is your constant pin value.

If you're adding your constraints programmatically, you can surely benefit from the identifier property in NSLayoutConstraint class. Set them and iterate over them to get that specific identifired constraint.

As the documentation says

extension NSLayoutConstraint {
    /* For ease in debugging, name a constraint by setting its identifier, which will be printed in the constraint's description.
     Identifiers starting with UI and NS are reserved by the system.
     */
    @availability(iOS, introduced=7.0)
    var identifier: String?
}
Ligure answered 3/2, 2015 at 7:58 Comment(5)
Thanks for the quick answer. I'm aware of this method but I just wonder can it be done programmatically? Cause I have a huge number of views and subviews so to wire them up one by one is going to make the whole thing ends up looking super messy and hard to maintain.Hedwig
If you're adding your constraints programmatically, you can surely benefit from the identifier property in NSLayoutConstraint class. Set them and iterate over them to get that specific identifired constraint.Ligure
Wow..This could be the one I'm looking for. Let me try when I get back to my mac. Thanks a lot for the help!Hedwig
this would be the same for ios swift, right? just making sureLilililia
@Lilililia yes it would.Ligure

© 2022 - 2024 — McMap. All rights reserved.