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

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.


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)