3
votes

I am trying to animate change UITableView height constraint using + transitionWithView:duration:options:animations:completion:. When the options is set, the UITableViewCell contentView's subview UIView also animates. The effect is like below. As you can see the cell underneath the window goes up and the red dot view's bounds animates to the predefined constraint. If I don't set the animation options, it won't be like this. But I need the animation when I change the UITableView height. So how to keep the table view height animation and disable the table view cell contentView's sub view animation? The code is below. The red dot view is a UIView with a red background color. Is there any way to disable UITableViewCell contentView's subview animation?

- (void)changeTableViewHeight {
    self.tableViewTopVerticalSpaceConstraint.constant = 0;
    [self.tableView setNeedsUpdateConstraints];
    [self.view setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.topViewHeightConstraint.constant = 50;
        [self.topView setNeedsUpdateConstraints];
    }];
}

animation

3

3 Answers

0
votes
//1. All constraints change must be done
self.hightConstraint.constant = 200;
self.anotherConstraint.constant = 34;
...
...

// 2. Indimate to iOS that this view need update constraints. 
// Hey iOS I have modified few constraints. beware of it to layout that changes.
[self.view setNeedsUpdateConstraints];


// 3. I'm done, relayout the view with animation, 
      If I really changed any constraints  to view.
[UIView animateWithDuration:1.5 animations:^{
    [self.view layoutIfNeeded];
}];

Hope somebody will save their time

0
votes

Had a similar problem that I solved with disabling animation for the layer of the view I didn't want animated. In your case ... implement the awakeFromNib method for your UITableViewCell subclass and disable the animations there. In your case something like ...

override func awakeFromNib() {
    super.awakeFromNib()
    redCircleView.layer.removeAllAnimations()
}

The thing is that UIView.animateWithDuration animates the views' layers and for new table view cells and other views created during or because of this animation, all changes will also be animated.

... EDIT ...

In the meantime I managed to find an even better solution. In the options parameter for the UIView.animateWithDuration use the UIViewAnimationOptionLayoutSubviews (or .LayoutSubviews in Swift) option. This layouts the subviews at animation commit time.

-1
votes

update code

- (void)changeTableViewHeight {
    self.tableViewTopVerticalSpaceConstraint.constant = 0;
    self.topViewHeightConstraint.constant = 50;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
       [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
       [self.topView setNeedsUpdateConstraints];
    }];
}