From 67271dc6667fae9633649422bc608ec731900a7f Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Sat, 10 Sep 2016 21:51:20 +1200 Subject: [PATCH] Rework how updateConstraints works if no existing constraints updateConstraints will no longer allow creation of new constraints but if you call updateConstraints and there have been no previous constraints created it will re-route to makeConstraints --- Source/Constraint.swift | 4 +--- Source/ConstraintMaker.swift | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 0bfce79..9928cde 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -225,8 +225,7 @@ public class Constraint { let layoutConstraints = self.layoutConstraints let existingLayoutConstraints = view.snp.layoutConstraints - if updatingExisting && existingLayoutConstraints.count > 0 { - + if updatingExisting { for layoutConstraint in layoutConstraints { let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint } guard let updateLayoutConstraint = existingLayoutConstraint else { @@ -236,7 +235,6 @@ public class Constraint { let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute) } - } else { NSLayoutConstraint.activate(layoutConstraints) view.snp.add(layoutConstraints: layoutConstraints) diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index 0749104..0ea2dd2 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -180,6 +180,11 @@ public class ConstraintMaker { } internal static func updateConstraints(view: ConstraintView, closure: (_ make: ConstraintMaker) -> Void) { + guard view.snp.layoutConstraints.count > 0 else { + self.makeConstraints(view: view, closure: closure) + return + } + let maker = ConstraintMaker(view: view) closure(maker) let constraints = maker.descriptions