-->

UITableView frame change animation issue

2020-07-16 12:11发布

问题:

I've googled about this problem a lot but there seems to be no answer. So I'm hoping some of you may know how to deal with this. I have a view controller that has a tableview, when I change the view frame with animation, everything goes well, except one particular case, when tableview has more items than it can fit to screen, and only when the tableview is scrolled to bottom. Then if I shrink the view height, my view animates correctly, but the tableview somehow jumps up a bit and only then animates to the bottom.

If I shrink the view, but tableview isn't scrolled to bottom (even if I can see the last cell, lets say a bit more than half of it) it does animate correctly.

I've tried couple of things, like setting autoresizing masks on and off and also animate from current state or something like that, but that didn't help :/

So any suggestions what could be the problem?

EDIT:

Code that i use to change frame

[UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{

                         [_contView setFrame:CGRectMake(0, 0, 320, 420)];
                     } 
                     completion:^(BOOL finished){

                     }];

回答1:

I used to have a similar problem and I implemented this workaround (Disclaimer: this is a hack to a currently-unresolved iOS bug):

// check if the table view is scrolled to the bottom
if (tableView.contentOffset.y + tableView.frame.size.height == tableView.contentSize.height) {
    // if it is, shift the table view contents up one pixel
    [tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y - 1) animated:NO];
}

// call the animation block afterwards here

It's a hack, though not noticeable to the user since it's just a 1 pixel motion. UITableView has a few bugs (reported to Apple already, but not yet resolved) when it comes to animations from a scrolled-to-bottom position.



回答2:

I fought this same bug for awhile before realizing I couldn't figure out for the life of me why it was happening...until I found a very detailed explanation and solution.

Full write-up can be found here: http://lists.apple.com/archives/cocoa-dev/2012/Apr/msg00341.html

Quick solution is to change contentInset instead of the table view frame. Just add the height of whatever you are showing (ie a keyboard or ad banner view) to the contentInset.bottom property. The scrolling area will be increased to account for the required resize.

In this example, I was showing an ad banner view on top of my tableview:

UIEdgeInsets insets = myTable.contentInset;
insets.bottom += 50;   // 50px is the height of the ad banner view
myTable.contentInset = insets;


回答3:

You should use autoresizing from xib which is right corner of xcode ... I think this may help you.



回答4:

Try to add footer view to your table with non-zero height.