4

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
nyus2006
  • 411
  • 6
  • 12
  • 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. – nyus2006 Dec 13 '13 at 17:18

3 Answers3

7

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.

nyus2006
  • 411
  • 6
  • 12
  • Depends. If you have a subview that is top-bottom-leading-trailing aligned with a superview, the subview is usually the firstItem for top and leading constraints, and the other way around fro the bottom and trailing constraints. – Ruiz Mar 02 '18 at 14:26
6

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 1
    thank you for your answer. I know how to do it with IB, was not sure how to do it programmatically. As the title of my question suggested:) – nyus2006 Dec 13 '13 at 17:19
  • This of course only works if you only need to modify ONE of the subviews. If there is an unknown number of subviews for which you will have to change the constraint, you will have to subclass the UIView you are using (button? label? cell?), add a property to it and set it when the constraints are being created. – Breno Gazzola Dec 13 '13 at 17:21
0

The Other way and easy one for me is to Ctrl+Drag the Constraint's Outlet to ViewController.m or ViewController.h file and you can set/get it's constant, priority,first item, second item etc.

Asadullah Ali
  • 1,056
  • 14
  • 31