Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support priority update #505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions Masonry/MASViewConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down