diff --git a/Snappy/Constraint.swift b/Snappy/Constraint.swift index f51d18c..f0b54c1 100644 --- a/Snappy/Constraint.swift +++ b/Snappy/Constraint.swift @@ -303,7 +303,7 @@ public class Constraint { // MARK: internal - internal func install() -> Array { + internal func install(updateExisting: Bool = false) -> Array { 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() + + // 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 diff --git a/Snappy/ConstraintMaker.swift b/Snappy/ConstraintMaker.swift index c5ca843..7bb609c 100644 --- a/Snappy/ConstraintMaker.swift +++ b/Snappy/ConstraintMaker.swift @@ -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() diff --git a/Snappy/LayoutConstraint.swift b/Snappy/LayoutConstraint.swift index c61068f..1588d81 100644 --- a/Snappy/LayoutConstraint.swift +++ b/Snappy/LayoutConstraint.swift @@ -32,4 +32,29 @@ 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 } \ No newline at end of file