Browse Source

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 năm trước cách đây
mục cha
commit
67271dc666

+ 1 - 3
Source/Constraint.swift

@@ -225,8 +225,7 @@ public class Constraint {
         let layoutConstraints = self.layoutConstraints
         let layoutConstraints = self.layoutConstraints
         let existingLayoutConstraints = view.snp.layoutConstraints
         let existingLayoutConstraints = view.snp.layoutConstraints
         
         
-        if updatingExisting && existingLayoutConstraints.count > 0 {
-            
+        if updatingExisting {
             for layoutConstraint in layoutConstraints {
             for layoutConstraint in layoutConstraints {
                 let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
                 let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
                 guard let updateLayoutConstraint = existingLayoutConstraint else {
                 guard let updateLayoutConstraint = existingLayoutConstraint else {
@@ -236,7 +235,6 @@ public class Constraint {
                 let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
                 let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
                 updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
                 updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
             }
             }
-            
         } else {
         } else {
             NSLayoutConstraint.activate(layoutConstraints)
             NSLayoutConstraint.activate(layoutConstraints)
             view.snp.add(layoutConstraints: 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) {
     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)
         let maker = ConstraintMaker(view: view)
         closure(maker)
         closure(maker)
         let constraints = maker.descriptions
         let constraints = maker.descriptions