Added support for updateConstraints

This commit is contained in:
Robert Payne 2015-01-08 19:20:34 +13:00
parent 5a968ae864
commit 9251b638af
3 changed files with 79 additions and 1 deletions

View File

@ -303,7 +303,7 @@ public class Constraint {
// MARK: internal
internal func install() -> Array<LayoutConstraint> {
internal func install(updateExisting: Bool = false) -> Array<LayoutConstraint> {
if self.installedOnView != nil {
NSException(name: "Cannot Install Constraint", reason: "Already installed", userInfo: nil).raise()
return []
@ -367,6 +367,42 @@ public class Constraint {
layoutConstraints.append(layoutConstraint)
}
// special logic for updating
if updateExisting {
// get existing constraints for this view
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
// array that will contain only new layout constraints
var newLayoutConstraints = Array<LayoutConstraint>()
// begin looping
for layoutConstraint in layoutConstraints {
// layout constraint that should be updated
var updateLayoutConstraint: LayoutConstraint? = nil
// loop through existing and check for match
for existingLayoutConstraint in existingLayoutConstraints {
if existingLayoutConstraint == layoutConstraint {
updateLayoutConstraint = existingLayoutConstraint
break
}
}
// if we have existing one lets just update the constant
if updateLayoutConstraint != nil {
updateLayoutConstraint!.constant = layoutConstraint.constant
}
// otherwise add this layout constraint to new list
else {
newLayoutConstraints.append(layoutConstraint)
}
}
// set constraints to only new ones
layoutConstraints = newLayoutConstraints
}
// add constarints
installOnView!.addConstraints(layoutConstraints)
self.installedOnView = installOnView

View File

@ -100,6 +100,23 @@ public class ConstraintMaker {
view.snp_installedLayoutConstraints = layoutConstraints
}
internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> ()) {
#if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else
view.translatesAutoresizingMaskIntoConstraints = false
#endif
let maker = ConstraintMaker(view: view)
block(make: maker)
var layoutConstraints = view.snp_installedLayoutConstraints
for constraint in maker.constraints {
layoutConstraints += constraint.install(updateExisting: true)
}
view.snp_installedLayoutConstraints = layoutConstraints
}
internal class func removeConstraints(view: View) {
for existingLayoutConstraint in view.snp_installedLayoutConstraints {
existingLayoutConstraint.constraint?.uninstall()

View File

@ -33,3 +33,28 @@ import AppKit
public class LayoutConstraint: NSLayoutConstraint {
internal var constraint: Constraint?
}
public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool {
if left.firstItem !== right.firstItem {
return false
}
if left.secondItem !== right.secondItem {
return false
}
if left.firstAttribute != right.firstAttribute {
return false
}
if left.secondAttribute != right.secondAttribute {
return false
}
if left.relation != right.relation {
return false
}
if left.priority != right.priority {
return false
}
if left.multiplier != right.multiplier {
return false
}
return true
}