Access Auto Layout Constraint Programmatically

女生的网名这么多〃 提交于 2019-12-06 07:48:48

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20572148/access-auto-layout-constraint-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!