O-mkar
O-mkar

Reputation: 5658

How to use constraint Identifiers in autolayout and how to change constrain using identifiers? [Swift]

While I was editing my constraint in Xcode 7 I found identifier field in Interface Builder. What is an identifier for the constraint how do I use it? using identifier can I access constraint programmatically and change the constant? my question is why and how is that identifier is helpful?

SNAPSHOT

Is there any way to access the constraint with an identifier with looping in subviews and again looping constrain subviews. I mean is there any way we can access constraint directly without a loop.

UPDATE

I Tryed this code but only width and height constrain is accessed

    for subview in view.subviews {
        for constraint in subview.constraints() {
           if constraint.identifier == "identifier" {
                return constraint
           }
        }
    }

Upvotes: 7

Views: 8518

Answers (3)

Ben Butterworth
Ben Butterworth

Reputation: 28522

To supplement the answer answer, we are also able to set constraints programmatically, and it was easier than I thought:

        let yConstraint = imageView.centerYAnchor.constraint(equalTo: layout.centerYAnchor)
        yConstraint.identifier = "yConstraint"
        let xConstraint = imageView.centerXAnchor.constraint(equalTo: layout.centerXAnchor)
            xConstraint.identifier = "xConstraint"

        NSLayoutConstraint.activate([
            yConstraint,
            xConstraint
        ])

Then, it showed up in the console, albeit difficult to read still...

<NSLayoutConstraint:0x6000025fe490 'xConstraint' UIImageView:0x7f8f41107fa0.centerX == UILayoutGuide:0x600003fcc1c0'UIViewLayoutMarginsGuide'.centerX   (active)>

Upvotes: 1

Romulo BM
Romulo BM

Reputation: 751

In Swift 4 it's possible:

With this extension:

extension UIView{
    func constraintWith(identifier: String) -> NSLayoutConstraint?{
        return self.constraints.first(where: {$0.identifier == identifier})
    }
}

And you can use't this way:

 if let myConstraint = myView.constraintWith(identifier: "myConstraintID"){
     myConstraint.constant = 8
 }

Ps: Be sure to call constraintWith(identifier: String) on the view where the constraint was added, normally on the superView of the related view you are changing, or on itself if the constraint is for size (width/height)

Upvotes: 5

Lawrence413
Lawrence413

Reputation: 2016

HOW: The identifier is useful when debugging (e.g. constraints not matching and one of them gets broken at run time; the constraint identifiers are being shown in log so you can see which one might cause problems)

WHY: Constraint identifiers make logs easier to read, more accurate and they save you a lot of time.

Constraint editing: If you want to change constraints programmatically you will have to declare them as outlets (like a label or button), then remove them from the view (NOT the object itself) and then set them again to the view. From my knowledge you can't just "edit" the constraints programmatically.

Your code gives you only width and height because you access the view's constraints which only contains the objects' widths and heights.

Upvotes: 7

Related Questions