Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Auto Layout Constraint Programmatically

I have a view that has a lot of subviews, I will refer to these views as superview, subview A, subview B, subview C, etc.

So I need to access the trailing space constraint I set on subView A to superview and modify it. This constraint would appear in superview.constraints.

However, all the subviews have leading/trailing space constraints set between them and the superview.

So, if I log superview.constraints, it would look like this:

<__NSArrayM 0xac744e0>(
<NSLayoutConstraint:0x98f3500 H:|-(0)-[UILabel:0x98f2190]   (Names:     '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3540 H:[UILabel:0x98f2190]-(20)-|   (Names:   '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3580 V:[UILabel:0x98f2190]-(-4)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f35c0 V:[UIView:0x98f2770]-(42)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3600 H:[UIView:0x98f2770]-(20)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3640 H:|-(0)-[UIView:0x98f2770]   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3680 H:|-(0)-[UIView:0x98f2610]   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f36c0 H:[UIView:0x98f2610]-(0)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3700 V:[UIView:0x98f2610]-(1)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3740 V:[UIView:0x98f2920]-(107)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3780 H:[UIView:0x98f2920]-(20)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f37c0 H:|-(0)-[UIView:0x98f2920]   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3800 H:[UIButton:0x98f19c0]-(178)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3840 V:[UIButton:0x98f19c0]-(55)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3880 H:|-(0)-[UILabel:0x98f2a80]   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f38c0 V:[UILabel:0x98f2a80]-(109)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3900 H:[UILabel:0x98f2a80]-(20)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3940 H:|-(171)-[UIButton:0x98f2ff0]   (Names: '|':HeaderReusableView:0x98f1f40 )>,
<NSLayoutConstraint:0x98f3980 V:[UIButton:0x98f2ff0]-(55)-|   (Names: '|':HeaderReusableView:0x98f1f40 )>

)

I have no idea which constraint is the one I want.

like image 218
nyus2006 Avatar asked Dec 11 '22 09:12

nyus2006


2 Answers

Ok I figured it out. NSLayoutConstraint has a property named firstItem and a property secondItem. These are the views that the constraints are set on.

Usually the secondItem would be the view itself. Thus, view.constraints[0].secondItem is view.

like image 175
nyus2006 Avatar answered Dec 26 '22 00:12

nyus2006


Make an IBOutlet to the one you're interested in. You can make those from the constraint in the canvas (sometimes that's hard to do), or from the list of constraints in the scene list on the left.

like image 28
rdelmar Avatar answered Dec 25 '22 22:12

rdelmar