Issam Otoz
Issam Otoz

Reputation: 67

get constraints of view

How to get leading, trailing, top and bottom constraints of a UIView, as shown in screenshot

enter image description here

Upvotes: 3

Views: 4628

Answers (2)

AamirR
AamirR

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
}

enter image description here

Upvotes: 6

Yervand Saribekyan
Yervand Saribekyan

Reputation: 502

You can do it with 3 ways

  1. With attribute
{view}.constraints.filter { $0.firstAttribute == .leading }
  1. With Idenetifier, You can give identifier to constraint from storyboard and getted as follows
{view}.constraints.filter { $0.identifier == "your identifier" }
  1. You cen set constraint's outlet in page

Upvotes: 6

Related Questions