获取NSLayoutConstraints关联视图 [英] getting NSLayoutConstraints associated view
问题描述
我试图遍历视图约束.
我在view1中添加了:顶部,尾部,前导和高度限制.
I added to view1: top, trailing, leading and height constraints.
顶部,尾部和前导是ViewControllers主视图.
top, trailing and leading are to the main ViewControllers view.
如果我遍历view1的约束,我只会看到高度约束.
if i loop through view1's constraints i only see the height constraint.
for constraint in view1.constraints {
print(constraint)
}
NSLayoutConstraint:0x6180000968a0 UIView:0x7fae6b409dd0.height == 146(活动)
NSLayoutConstraint:0x6180000968a0 UIView:0x7fae6b409dd0.height == 146 (active)
因此我遍历了其超级视图约束(ViewControllers主视图),并且遇到了很多约束,其中一些约束与view1相关.
so i looped through its superviews constraints (the ViewControllers main view) and i got lots of constraints some of them are associated with view1.
for constraint in view1.superview?.constraints {
print(constraint)
}
NSLayoutConstraint:0x618000096670 H:|-(0)-[UIView:0x7fae6b409dd0](活动,名称:'|':UIView:0x7fae6b40a180)
NSLayoutConstraint:0x618000096670 H:|-(0)-[UIView:0x7fae6b409dd0] (active, names: '|':UIView:0x7fae6b40a180 )
NSLayoutConstraint:0x6180000974d0 H:[UIView:0x7fae6b409dd0]-(0)-| (活动,名称:"|":UIView:0x7fae6b40a180)
NSLayoutConstraint:0x6180000974d0 H:[UIView:0x7fae6b409dd0]-(0)-| (active, names: '|':UIView:0x7fae6b40a180 )
NSLayoutConstraint:0x618000097520 V:|-(0)-[UIView:0x7fae6b409dd0](活动,名称:'|':UIView:0x7fae6b40a180)
NSLayoutConstraint:0x618000097520 V:|-(0)-[UIView:0x7fae6b409dd0] (active, names: '|':UIView:0x7fae6b40a180 )
还有一些我不在乎的东西.
and i get a few more that i dont care about.
所以我的问题是我想遍历view1的所有superviews约束,而只获取与之关联的约束.
So my problem is that i want to loop through all of view1's superviews constraints and get only the ones that are associated with it.
在此示例中,UIView:0x7fae6b409dd0
是view1.
In this example UIView:0x7fae6b409dd0
is view1.
但是我不知道如何获得该财产.
But i cant figure out how to get that property.
谢谢
如果我打印出constraint.firstAnchor
,我可以获得更多信息,但仍无法获取相关视图.
If i print out constraint.firstAnchor
i get some more information but still cant get the associated view.
NSLayoutXAxisAnchor:0x608000265480"UIView:0x7fae6b409dd0.leading">
NSLayoutXAxisAnchor:0x608000265480 "UIView:0x7fae6b409dd0.leading">
NSLayoutXAxisAnchor:0x608000265480"UIView:0x7fae6b409dd0.trailing">
NSLayoutXAxisAnchor:0x608000265480 "UIView:0x7fae6b409dd0.trailing">
NSLayoutXAxisAnchor:0x608000265480"UIView:0x7fae6b409dd0.top">
NSLayoutXAxisAnchor:0x608000265480 "UIView:0x7fae6b409dd0.top">
推荐答案
您可以使用NSLayoutConstraint
的firstItem
和secondItem
属性来获取与约束相关的视图.请注意,secondItem
是可选,并且必须解包.
You can use the firstItem
and secondItem
properties of NSLayoutConstraint
to get the views related to the constraint. Note that secondItem
is an Optional and must be unwrapped.
然后,您可以使用===
运算符比较它是否是同一对象:
Then you can use the ===
operator to compare if it is the same object:
let constraints = view1.superview!.constraints
var count = 0
print("superview has \(constraints.count) constraints")
for constraint in constraints {
if constraint.firstItem === view1 {
count += 1
print(constraint)
} else if let secondItem = constraint.secondItem, secondItem === view1 {
count += 1
print(constraint)
}
}
print("\(count) of them relate to view1")
这篇关于获取NSLayoutConstraints关联视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!