Skip to content

Instantly share code, notes, and snippets.

@priore
Last active February 28, 2016 02:51
Show Gist options
  • Save priore/41f0d70c764be8ff294c to your computer and use it in GitHub Desktop.
Save priore/41f0d70c764be8ff294c to your computer and use it in GitHub Desktop.
Change value of existing NSLayoutConstraints
- (void)setConstraintValue:(CGFloat)value forAttribute:(NSLayoutAttribute)attribute animation:(BOOL)animation
{
NSArray *constraints = self.view.constraints;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
NSLayoutConstraint *constraint = [filteredArray firstObject];
if (constraint.constant != value) {
[self.view removeConstraint:constraint];
constraint.constant = value;
[self.view addConstraint:constraint];
if (animation) {
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
} else
[self.view layoutIfNeeded];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment