Prechádzať zdrojové kódy

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
Robert Payne 9 rokov pred
rodič
commit
67271dc666
2 zmenil súbory, kde vykonal 6 pridanie a 3 odobranie
  1. 1 3
      Source/Constraint.swift
  2. 5 0
      Source/ConstraintMaker.swift

+ 1 - 3
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)

+ 5 - 0
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