Reputation: 67
How to get leading, trailing, top and bottom constraints of a UIView, as shown in screenshot
Upvotes: 3
Views: 4628
Reputation: 12198
Its better and easier to set an identifier for the constraint, as shown in screenshot below, and use following
if let topConstraint = button.constraints.first(where: { $0.identifier == "topConstraint" }) {
... use topConstraint
}
Another way:
if let topConstraint = button.constraints.first(where: { ($0.firstAttribute == .top && $0.firstItem === button) || ($0.secondAttribute == .top && $0.secondItem === button) }) {
... use topConstraint
}
Upvotes: 6
Reputation: 502
You can do it with 3 ways
{view}.constraints.filter { $0.firstAttribute == .leading }
{view}.constraints.filter { $0.identifier == "your identifier" }
Upvotes: 6