Ensure constraints get cleaned up on uninstall() and put back on install()

This commit is contained in:
Robert Payne 2015-01-14 14:16:26 +13:00
parent cf977c5a1a
commit 0b92c7ff54
2 changed files with 12 additions and 2 deletions

View File

@ -380,8 +380,9 @@ public class Constraint {
layoutConstraints = newLayoutConstraints layoutConstraints = newLayoutConstraints
} }
// add constarints // add constraints
installOnView!.addConstraints(layoutConstraints) installOnView!.addConstraints(layoutConstraints)
installOnView!.snp_installedLayoutConstraints = layoutConstraints
self.installedOnView = installOnView self.installedOnView = installOnView
return layoutConstraints return layoutConstraints
@ -405,6 +406,15 @@ public class Constraint {
if constraintsToRemove.count > 0 { if constraintsToRemove.count > 0 {
view.removeConstraints(constraintsToRemove) view.removeConstraints(constraintsToRemove)
} }
var layoutConstraints = view.snp_installedLayoutConstraints
var layoutConstraintsToKeep = Array<LayoutConstraint>()
for layoutConstraint in layoutConstraints {
if !contains(constraintsToRemove, layoutConstraint) {
layoutConstraintsToKeep.append(layoutConstraint)
}
}
view.snp_installedLayoutConstraints = layoutConstraintsToKeep
} }
self.installedOnView = nil self.installedOnView = nil
} }

View File

@ -111,7 +111,7 @@ public class ConstraintMaker {
var layoutConstraints = view.snp_installedLayoutConstraints var layoutConstraints = view.snp_installedLayoutConstraints
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.install(updateExisting: true) layoutConstraints += constraint.installOnView(updateExisting: true)
} }
view.snp_installedLayoutConstraints = layoutConstraints view.snp_installedLayoutConstraints = layoutConstraints