Get reference to NSLayoutConstraint using Identifier set in storyboard
You may wish to extrapolate the logic provided in previous answer into an extension.
extension UIView {
/// Returns the first constraint with the given identifier, if available.
///
/// - Parameter identifier: The constraint identifier.
func constraintWithIdentifier(_ identifier: String) -> NSLayoutConstraint? {
return self.constraints.first { $0.identifier == identifier }
}
}
You can then access any constraint anywhere using:
myView.constraintWithIdentifier("myConstraintIdentifier")
Edit: Just for kicks, using the above code, here's an extension that finds all the constraints with that identifier in all the child UIViews. Had to change the function to return an array instead of the first constraint found.
func constraintsWithIdentifier(_ identifier: String) -> [NSLayoutConstraint] {
return self.constraints.filter { $0.identifier == identifier }
}
func recursiveConstraintsWithIdentifier(_ identifier: String) -> [NSLayoutConstraint] {
var constraintsArray: [NSLayoutConstraint] = []
var subviews: [UIView] = [self]
while !subviews.isEmpty {
constraintsArray += subviews.flatMap { $0.constraintsWithIdentifier(identifier) }
subviews = subviews.flatMap { $0.subviews }
}
return constraintsArray
}
Swift 3
I wrote a quick NSView extension which handles this nicely.
extension NSView {
func constraint(withIdentifier: String) -> NSLayoutConstraint? {
return self.constraints.filter { $0.identifier == withIdentifier }.first
}
}
Usage:
if let c = button.constraint(withIdentifier: "my-button-width") {
// do stuff with c
}
I assume you have an outlet set up for the button so you have an available reference to it. So first, retrieve the view's constraints from your button. Then loop through the array and on each iteration compare the identifer property of each constraint with the value you entered in Interface Builder. Looks like you are coding in Objective-C, so Objective-C code sample is below. Change @"identifier" to whatever value you set in Interface Builder.
NSArray *constraints = [button constraints];
int count = [constraints count];
int index = 0;
BOOL found = NO;
while (!found && index < count) {
NSLayoutConstraint *constraint = constraints[index];
if ( [constraint.identifier isEqualToString:@"identifier"] ) {
//save the reference to constraint
found = YES;
}
index++;
}
In Swift 3,
let filteredConstraints = button.constraints.filter { $0.identifier == "identifier" }
if let yourConstraint = filteredConstraints.first {
// DO YOUR LOGIC HERE
}