Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout: animating changes to NSLayoutConstraint's multiplier

I have this simple view:

- (void)viewDidLoad
{
    [super viewDidLoad];
    redBox = [[UIView alloc] init];
    [redBox setBackgroundColor:[UIColor redColor]];
    [redBox setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:redBox];

    //H:
    widthConstraint = [NSLayoutConstraint constraintWithItem:redBox
                                                   attribute:NSLayoutAttributeWidth
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeWidth
                                                  multiplier:0.5
                                                    constant:0.0];
    [self.view addConstraint:widthConstraint];

    //More constraints... 
}

I’d like to animate an increase in redBox's width.

I’ve tried this:

widthConstraint.constant = 100;

[UIView animateWithDuration:1
                 animations:^{
                     [self.view layoutIfNeeded];
                 }
 ];

this increases the view’s width by 100 and animates it, but I need to increase it by an amount proportional to its superview’s width. In other words, I’d like to set widthConstraint’s multiplier to 0.8.

But NSLayoutConstraint’s multiplier is readonly! Does this mean I have to remove, modify and re-add widthConstraint every time I want to animate a change to its multiplier or is there a better way?

like image 502
Eric Avatar asked Dec 11 '13 15:12

Eric


Video Answer


2 Answers

Unfortunately, there is no better way. Removing and re-adding with a different multiplier is your only option. Please file a bug that you would like to see multiplier to be readwrite.

like image 124
Scott Berrevoets Avatar answered Sep 28 '22 03:09

Scott Berrevoets


I asked this question before. And we are found only one way, it's remove constraints and added it later with modify multipler. @"It's Apple baby"(©My Boss)

UPD

look at Masonry mb you can replace multipler on plain constrain with constant.

like image 26
Bimawa Avatar answered Sep 28 '22 03:09

Bimawa