From 44f8980262f47f0f24df40e840fe25bd3d859b13 Mon Sep 17 00:00:00 2001 From: lazy-iOS2 Date: Mon, 1 Jan 2018 22:38:45 +0800 Subject: [PATCH] Support priority update Add Support for priority update --- Masonry/MASViewConstraint.m | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Masonry/MASViewConstraint.m b/Masonry/MASViewConstraint.m index af3f0b44..f8740bc1 100644 --- a/Masonry/MASViewConstraint.m +++ b/Masonry/MASViewConstraint.m @@ -163,10 +163,17 @@ - (void)setSecondViewAttribute:(id)secondViewAttribute { - (MASConstraint * (^)(MASLayoutPriority))priority { return ^id(MASLayoutPriority priority) { - NSAssert(!self.hasBeenInstalled, - @"Cannot modify constraint priority after it has been installed"); - - self.layoutPriority = priority; + if (!self.hasBeenInstalled) { + self.layoutPriority = priority; + } else { + NSAssert(priority != UILayoutPriorityRequired + && self.layoutConstraint.priority != UILayoutPriorityRequired, + @"the priority cannot be changed from/to NSLayoutPriorityRequired"); + if (priority != UILayoutPriorityRequired + && self.layoutConstraint.priority != UILayoutPriorityRequired){ + self.layoutConstraint.priority = priority; + } + } return self; }; } @@ -359,8 +366,20 @@ - (void)install { existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint]; } if (existingConstraint) { - // just update the constant + // update the constant existingConstraint.constant = layoutConstraint.constant; + + // update the priority if possible + if (existingConstraint.priority != layoutConstraint.priority) { + NSAssert(existingConstraint.priority != UILayoutPriorityRequired + && layoutConstraint.priority != UILayoutPriorityRequired, + @"the priority cannot be changed from/to NSLayoutPriorityRequired"); + if (existingConstraint.priority != UILayoutPriorityRequired + && layoutConstraint.priority != UILayoutPriorityRequired){ + existingConstraint.priority = layoutConstraint.priority; + } + } + self.layoutConstraint = existingConstraint; } else { [self.installedView addConstraint:layoutConstraint]; @@ -382,7 +401,6 @@ - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layout if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue; if (existingConstraint.relation != layoutConstraint.relation) continue; if (existingConstraint.multiplier != layoutConstraint.multiplier) continue; - if (existingConstraint.priority != layoutConstraint.priority) continue; return (id)existingConstraint; }