Eric
Eric

Reputation: 16931

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?

Upvotes: 10

Views: 6074

Answers (2)

Bimawa
Bimawa

Reputation: 3720

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.

Upvotes: 2

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

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.

Upvotes: 7

Related Questions