简体   繁体   中英

UIView not animating constraint change

I have a question regarding the UIView method:

+ (void)animateWithDuration:(NSTimeInterval)duration
             animations:(void (^)(void))animations
             completion:(void (^)(BOOL finished))completion

Basically I am just trying to set a height constraints constant to 0 and have the change look like its slowly shrinking...

My Code looks like this:

[UIView animateWithDuration:0.7
                     animations:^{
                         //hide the Title
                         self.titleCellHeigthConstraint.constant = kLabelHeightZero;
                         [self.contentView layoutIfNeeded];
                     }

                     completion:nil];

Unfortunately it's not animating. It simply makes all changes appear with a little delay (which might be the 0.7 seconds specified in the method).

Now my question is if there is any way to get the UIView to animate the change?

Thanks in Advance!

Edit:

The weird thing is if I reverse the changes in an animated fashion it does it exactly the way I want it. The code I'am using for that is:

//animate layout changes
    [UIView animateWithDuration:0.7
                     animations:^{
                         //hide activity indicator and label
                         [self.activityIndicator stopAnimating];
                         [self.label setHidden:YES];
                     }

                     completion:^(BOOL arg0){
                         [UIView animateWithDuration:0.3
                                          animations:^{
                                              //show title label
                                              self.titleCellHeigthConstraint.constant = kDefaultLabelHeight;
                                              [self.contentView layoutIfNeeded];
                                          }];
                     }];

You want to change the properties of the UIView not the NSLayoutConstraint in the animation method. Ie Change the layout constraint's constant before the UIView method and then change the UIView's frame property in the animation method.

This way you won't need to call layoutIfNeeded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM