简体   繁体   English

获取 NSLayoutConstraints 关联视图

[英]getting NSLayoutConstraints associated view

I trying to loop through a views constraints.我试图遍历视图约束。

I added to view1: top, trailing, leading and height constraints.我添加到 view1:顶部、尾部、前导和高度约束。

top, trailing and leading are to the main ViewControllers view.顶部、尾随和前导是主 ViewControllers 视图。

if i loop through view1's constraints i only see the height constraint.如果我遍历 view1 的约束,我只会看到高度约束。

for constraint in view1.constraints {
    print(constraint)
}

NSLayoutConstraint:0x6180000968a0 UIView:0x7fae6b409dd0.height == 146 (active) NSLayoutConstraint:0x6180000968a0 UIView:0x7fae6b409dd0.height == 146 (active)

so i looped through its superviews constraints (the ViewControllers main view) and i got lots of constraints some of them are associated with view1.所以我遍历了它的 superviews 约束(ViewControllers 主视图),我得到了很多约束,其中一些与 view1 相关联。

for constraint in view1.superview?.constraints {
    print(constraint)
}

NSLayoutConstraint:0x618000096670 H:|-(0)-[UIView:0x7fae6b409dd0] (active, names: '|':UIView:0x7fae6b40a180 ) NSLayoutConstraint:0x618000096670 H:|-(0)-[UIView:0x7fae6b409dd0](活动,名称:'|':UIView:0x7fae6b40a180)

NSLayoutConstraint:0x6180000974d0 H:[UIView:0x7fae6b409dd0]-(0)-| NSLayoutConstraint:0x6180000974d0 H:[UIView:0x7fae6b409dd0]-(0)-| (active, names: '|':UIView:0x7fae6b40a180 ) (活动,名称:'|':UIView:0x7fae6b40a180)

NSLayoutConstraint:0x618000097520 V:|-(0)-[UIView:0x7fae6b409dd0] (active, names: '|':UIView:0x7fae6b40a180 ) NSLayoutConstraint:0x618000097520 V:|-(0)-[UIView:0x7fae6b409dd0](活动,名称:'|':UIView:0x7fae6b40a180)

and i get a few more that i dont care about.我还有一些我不关心的。

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.所以我的问题是我想遍历所有 view1 的 superviews 约束并只获取与其关联的那些。

In this example UIView:0x7fae6b409dd0 is view1.在这个例子中UIView:0x7fae6b409dd0是 view1。

But i cant figure out how to get that property.但我无法弄清楚如何获得该财产。

Thanks谢谢


If i print out constraint.firstAnchor i get some more information but still cant get the associated view.如果我打印出constraint.firstAnchor我会获得更多信息,但仍然无法获得相关视图。

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">

You can use the firstItem and secondItem properties of NSLayoutConstraint to get the views related to the constraint.您可以使用firstItemsecondItem的性能NSLayoutConstraint获得相关约束的意见。 Note that secondItem is an Optional and must be unwrapped.请注意, secondItem是一个Optional并且必须展开。

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")

一个简单的方法是比较约束对象中是否存在视图的哈希码:

[[NSString stringWithFormat:@"%@", constraint] containsString:[NSString stringWithFormat:@"%x", view.hash]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM