18

If I attempt to animate the frame height of a tableView (ex: height -= 200), the cells that appear in the last 200px disappear suddenly before the smooth animation of the frame completes.

To make sure that it's nothing else I'm doing, I created a new View-Based application. In the main viewController I create my own tableview with enough pseudo rows to fill the entire screen. And on selection of a row I do a simple height animation.

most relevant code:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
    myTable.delegate = self;
    myTable.dataSource = self;
    [self.view addSubview:myTable];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CGRect frame = self.myTable.frame;
    frame.size.height = 200;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelay:.5f];
    [UIView setAnimationDuration:0.5f];
    self.myTable.frame = frame;
    [UIView commitAnimations];
}

Does anyone know why this is happening, or what a fix/workaround may be?

Any suggests are really appreciated. TIA!

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
dizy
  • 7,951
  • 10
  • 53
  • 54

8 Answers8

15

I'm not sure I had exactly the same problem, but using [UIView setAnimationBeginsFromCurrentState:YES]; solved (parts of) the glitches (my table view slid around crazily when animating a frame height change).

ryyst
  • 9,563
  • 18
  • 70
  • 97
  • 1
    thanks. solved it for me. problem: animaiting outer view (frame) - and resizing an inner textview (only new height - pos shall stay the same) – Alex Milde Oct 05 '11 at 14:24
8

Same problem here as well. This is what I'm doing and the origin animates smoothly, but the size changes immediately... annoying.

    [UIView beginAnimations:@"HideTabbar" context:nil];
    [UIView setAnimationDuration:.3];
        self.tableView.frame = CGRectMake(0.0, 44.0, 320, 366);
    [UIView commitAnimations];

UPDATE: Try adding this before the animation:

self.tableView.autoresizingMask = UIViewAutoresizingNone;
Jonah
  • 4,810
  • 14
  • 63
  • 76
4

For anyone hitting this question & answer in the future, here's how to use @ryyst's answer in UIView's block based animations introduced in iOS 4.

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            // change frame etc here
    } completion:^(BOOL finished) {
            // any clean up here
    }];
Dave Wood
  • 13,143
  • 2
  • 59
  • 67
3

This is an old question and there are already a couple suggestions for a workaround, but I thought I'd add mine.

I ended up animating the contentInset and scrollIndicatorInsets properties, which provides the illusion that the table itself is being resized.

Edward Dale
  • 29,597
  • 13
  • 90
  • 129
1

I have exactly the same problem. I imagine that tableviews have a special behavior on "setFrame:", it seems that the tableview remove the cells that won't be visible with the new frame.

In case of an animation, the cells won't be visible only at the end of the animation, but it seems that tableviews don't care.

If someone have a better theory, I'd be glad to hear it !

Unfalkster
  • 683
  • 5
  • 12
  • After days of research, this seems to be the right theory. I am not sure if anything changed in the past 9 years :) Have you seen any fix for this? – Guven Jun 04 '18 at 08:00
0

I found your question while seeking the proper method to resize a tableView. I think your problem is in your animation you've specified UIView instead of UITableView. I was unable to duplicate your problem using either UIView or UITableView, but I'm using SDK 3.1 and it might be a bug that has been fixed since your post. I'm not sure, but I hope this helps!

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
  • No, it's correct to use "UIView" methods for the animation calls. – rluba Oct 05 '09 at 07:46
  • I tried the same code with 3.1 as well, and still an issue for me ;/ My current workaround is to wait for the animation to complete before resizing the tableview – dizy Oct 14 '09 at 07:31
0

It's an old question but this might help someone in the future;

I solved a similar problem by embedding the tableview in a UIView, and resizing the UIView instead of tableview. I set the tableview's height to a large value and also "clip subviews" property on the UIView. I resize the UIView proportional to tableview's contentSize. Its not a good solution but it worked for my purposes.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
feluna
  • 85
  • 2
  • 10
  • this will cause another problem, you wont be able to see tableView's few last cells because they are inside tableView's frame but far below the containerUIView's visible frame, so tableView will bounce when you try to scroll it because it has reached the end of the cells and yet still you can't see them – Mehdi Ijadnazar Aug 22 '16 at 07:10
0

Finally found the solution! there is indeed a bug!! don't use variables, when animating the height use [[UIScreen mainScreen] bounds]. Like this:

[UIView animateWithDuration:0.5 animations:^{
            tableView.frame=CGRectMake(0, 38, fullScreenRect.size.width, [[UIScreen mainScreen] bounds].size.height-38);
}];

not :

[UIView animateWithDuration:0.5 animations:^{
            tableView.frame=CGRectMake(0, 38, fullScreenRect.size.width, fullScreenRect.size.width-38);
}];

and it will work like magic!

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30