Skip to content

Instantly share code, notes, and snippets.

@JasonCanCode
Last active March 20, 2017 17:58
Show Gist options
  • Save JasonCanCode/d0b0c0b708d348f0e55d08bcb0f64306 to your computer and use it in GitHub Desktop.
Save JasonCanCode/d0b0c0b708d348f0e55d08bcb0f64306 to your computer and use it in GitHub Desktop.
Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
import UIKit
extension UIView {
/// Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
func constraint(withIdentifier identifier: String) -> NSLayoutConstraint? {
if let constraint = superview?.constraintFiltered(identifier) {
return constraint
} else if let constraint = constraintFiltered(identifier) {
return constraint
} else {
return checkSubviews(subviews, forIdentifier: identifier)
}
}
private func checkSubviews(_ views: [UIView], forIdentifier identifier: String) -> NSLayoutConstraint? {
if views.isEmpty { return nil }
for view in views {
if let constraint = view.constraintFiltered(identifier) {
return constraint
} else if let constraint = checkSubviews(view.subviews, forIdentifier: identifier) {
return constraint
}
}
return nil
}
private func constraintFiltered(_ identifier: String) -> NSLayoutConstraint? {
return constraints.filter({$0.identifier == identifier}).first
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment