Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2015 23:07
Show Gist options
  • Save anonymous/2ef9e04f237b889459ce to your computer and use it in GitHub Desktop.
Save anonymous/2ef9e04f237b889459ce to your computer and use it in GitHub Desktop.
UIView extension - Fallback Constraints
view.addFallbackConstraints(item: labelB, attribute: .Top, relatedBy: .Equal, fallbackArrayItems:[topImageView], attribute: .Bottom, multiplier: 1, constant: 20, startPriority:110)
view.addFallbackConstraints(item: labelC, attribute: .Top, relatedBy: .Equal, fallbackArrayItems:[labelA,topImageView], attribute: .Bottom, multiplier: 1, constant: 20, startPriority:105)
view.addFallbackConstraints(item: labelD, attribute: .Top, relatedBy: .Equal, fallbackArrayItems:[labelB,labelA,topImageView], attribute: .Bottom, multiplier: 1, constant: 20, startPriority:100)
extension UIView {
func addFallbackConstraints(item view1: AnyObject, attribute attr1: NSLayoutAttribute, relatedBy relation: NSLayoutRelation, fallbackArrayItems: [AnyObject], attribute attr2: NSLayoutAttribute, multiplier: CGFloat = 1, constant c: CGFloat, startPriority: UILayoutPriority) -> UILayoutPriority {
var priorityDecrementer = startPriority
for item in fallbackArrayItems {
var constraint = NSLayoutConstraint(item: view1, attribute: attr1, relatedBy: relation, toItem: item, attribute: attr2, multiplier: multiplier, constant: c)
constraint.priority = priorityDecrementer--
self.addConstraint(constraint)
}
return priorityDecrementer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment