以编程方式访问自动布局约束 [英] Access Auto Layout Constraint Programmatically
问题描述
我有一个有很多子视图的视图,我将这些视图称为superview,子视图A,子视图B,子视图C等。
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.
所以我需要访问我在subView A上设置的尾随空间约束来superview并修改它。此约束将出现在superview.constraints中。
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.
但是,所有子视图都在它们和superview之间设置了前导/尾随空间约束。
However, all the subviews have leading/trailing space constraints set between them and the superview.
所以,如果我记录superview.constraints,它看起来像这样:
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.
推荐答案
好的我明白了。
NSLayoutConstraint有一个名为firstItem的属性和一个属性secondItem。这些是设置约束的视图。
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.
通常,secondItem将是视图本身。因此,view.constraints [0] .secondItem是视图。
Usually the secondItem would be the view itself. Thus, view.constraints[0].secondItem is view.
这篇关于以编程方式访问自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!