Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Change value of existing NSLayoutConstraints
       
back

- (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 && 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];
    }
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com