Last active
March 20, 2017 17:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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