Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating view width whilst using auto layout

I am using auto layout to position a custom progress bar (its simply a UIView with a background colour). Im not using a proper progress bar because I didnt want to have to fully customise it to my needs when a UIView will suffice.

Anyway I have told XCode to place the progress bar to the left, and a certain distance from the bottom. This positions it correctly but when I animate the width nothing happens. If I create a new UIVIew then animate that it works correctly. Are the constraints preventing this from animating? No constrains is placed upon the width at all

[UIView animateWithDuration:startingGameSeconds delay:0.0 options:UIViewAnimationOptionCurveLinear
                 animations:^(void) {
                     progressBar.frame = CGRectMake(progressBar.frame.origin.x,
                                                    progressBar.frame.origin.y,
                                                    320,
                                                    progressBar.frame.size.height);

                 }
                 completion:^(BOOL finished){
                 }];
like image 469
Chris Avatar asked Feb 15 '23 07:02

Chris


1 Answers

When you're using Auto Layout you shouldn't be using the setFrame: method anymore. When you want to animate the 'frame', you'll have to animate the constraints. I suppose your Auto Layout constraints are defined in Interface Builder. First, make sure you have a width layout constraint added to your progress bar. Next, create an IBOutlet for this width layout constraint. You can now animate the constraint like this:

self.progressBarWidthLayoutConstraint.constant = 320;

[UIView animateWithDuration:1 animations:^{
    [self.view layoutIfNeeded];
}];
like image 68
s1m0n Avatar answered Feb 27 '23 02:02

s1m0n