From c75495cf4c8788178f9fd7fd20d5c17fc47084c8 Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Tue, 27 Jan 2015 00:31:17 +1300 Subject: [PATCH 1/2] Allow size attributes to be installed on a view itself --- Snap/Constraint.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Snap/Constraint.swift b/Snap/Constraint.swift index f034053..b8f09ed 100644 --- a/Snap/Constraint.swift +++ b/Snap/Constraint.swift @@ -297,8 +297,14 @@ public class Constraint { } else { installOnView = self.fromItem.view?.superview if installOnView == nil { + if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height { + installOnView = self.fromItem.view + } + + if installedOnView == nil { NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise() return [] + } } } From 0b687e37bd6aa508cdce6fa629da537c7f57e924 Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Tue, 27 Jan 2015 01:27:14 +1300 Subject: [PATCH 2/2] Reworked how constraints are stored against installed view --- Snap/Constraint.swift | 57 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/Snap/Constraint.swift b/Snap/Constraint.swift index 68f6d00..c9fa8ae 100644 --- a/Snap/Constraint.swift +++ b/Snap/Constraint.swift @@ -268,13 +268,13 @@ public class Constraint { self.offset = amount return self } - + // MARK: install / uninstall - + public func install() -> Array { return self.installOnView(updateExisting: false) } - + public func uninstall() { self.uninstallFromView() } @@ -297,14 +297,14 @@ public class Constraint { } else { installOnView = self.fromItem.view?.superview if installOnView == nil { - if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height { - installOnView = self.fromItem.view - } - - if installedOnView == nil { - NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise() - return [] - } + if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height { + installOnView = self.fromItem.view + } + + if installedOnView == nil { + NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise() + return [] + } } } @@ -388,40 +388,40 @@ public class Constraint { // add constraints installOnView!.addConstraints(layoutConstraints) - + self.installedOnView = installOnView + self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable() + for layoutConstraint in layoutConstraints { + self.installedLayoutConstraints!.addObject(layoutConstraint) + } + return layoutConstraints } internal func uninstallFromView() { if let view = self.installedOnView { - #if os(iOS) - var installedConstraints = view.constraints() - #else - var installedConstraints = view.constraints - #endif - var constraintsToRemove: Array = [] - for installedConstraint in installedConstraints { - if let layoutConstraint = installedConstraint as? LayoutConstraint { - if layoutConstraint.constraint === self { - constraintsToRemove.append(layoutConstraint) - } - } + // remove all installed layout constraints + var layoutConstraintsToRemove = Array() + if let installedLayoutConstraints = self.installedLayoutConstraints?.allObjects as? Array { + layoutConstraintsToRemove += installedLayoutConstraints } - if constraintsToRemove.count > 0 { - view.removeConstraints(constraintsToRemove) + + if layoutConstraintsToRemove.count > 0 { + view.removeConstraints(layoutConstraintsToRemove) } - + + // clean up the snp_installedLayoutConstraints var layoutConstraints = view.snp_installedLayoutConstraints var layoutConstraintsToKeep = Array() for layoutConstraint in layoutConstraints { - if !contains(constraintsToRemove, layoutConstraint) { + if !contains(layoutConstraintsToRemove, layoutConstraint) { layoutConstraintsToKeep.append(layoutConstraint) } } view.snp_installedLayoutConstraints = layoutConstraintsToKeep } self.installedOnView = nil + self.installedLayoutConstraints = nil } // MARK: private @@ -434,6 +434,7 @@ public class Constraint { private var priority: Float = 1000.0 private var offset: Any? + private var installedLayoutConstraints: NSHashTable? private weak var installedOnView: View? private func addConstraint(attributes: ConstraintAttributes) -> Constraint {