Advertisement
priore

Change value of existing NSLayoutConstraints

Feb 18th, 2016
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)setConstraintValue:(CGFloat)value forAttribute:(NSLayoutAttribute)attribute animation:(BOOL)animation
  2. {
  3.     NSArray *constraints = self.view.constraints;
  4.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
  5.     NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
  6.     NSLayoutConstraint *constraint = [filteredArray firstObject];
  7.     if (constraint && constraint.constant != value) {
  8.         [self.view removeConstraint:constraint];
  9.         constraint.constant = value;
  10.         [self.view addConstraint:constraint];
  11.        
  12.         if (animation) {
  13.             [UIView animateWithDuration:1.0 animations:^{
  14.                 [self.view layoutIfNeeded];
  15.             }];
  16.         } else
  17.             [self.view layoutIfNeeded];
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement