Skip to content

Instantly share code, notes, and snippets.

@alsedi
Created March 16, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsedi/dfcaed5e20da361d82b0 to your computer and use it in GitHub Desktop.
Save alsedi/dfcaed5e20da361d82b0 to your computer and use it in GitHub Desktop.
Easy access to constraints by Identifier
//
// UIView+Contstraints.swift
//
// HOW TO USE
// 1. Setup constraints and define NSLayoutConstraint.identifier
// 2. Use identifier as parameter for call
// 3. Call on superview: if let constraint = self.view.constraintByStringId("Id") { }
// 4. Change, animate or replace constraint
import Foundation
import UIKit
extension UIView {
func constraintByStringId(stringId: String) -> NSLayoutConstraint? {
for constraint in self.constraints {
if constraint.identifier == stringId {
return constraint
}
}
return nil
}
func constraintsByStringId(stringId: String) -> [NSLayoutConstraint]? {
var result = [NSLayoutConstraint]()
for constraint in self.constraints {
if constraint.identifier == stringId {
result.append(constraint)
}
}
return result.count == 0 ? nil : result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment