Last active
February 28, 2016 02:51
-
-
Save priore/41f0d70c764be8ff294c to your computer and use it in GitHub Desktop.
Change value of existing NSLayoutConstraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (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