Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UITableView height, and keep scrolled at bottom

Tags:

ios

Im trying to resize the tableview's height with a animation, it works fine by animating the tableview's frame.size.height.

The problem is, i have a tableview that is 200px height and scrolled to the bottom, i want to animate this to 100px, i run a simple

[UIView animateWithDuration:0.245f animations:^{
 CGRect frame = tableview.frame;
 frame.size.height = 100.f;
 tableview.frame = frame;
}];

this works fine, but after i resized it it is no longer scrolled to the bottom of the tableview. i want the tableview to always be scrolled at the bottom while animating. i tried alot of differnet things like calling

[tablview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

right before/after i start the resize animation, but i have not managed to get them to sync 100%. Is there a way to resize the tableview while the bottom of the tableview displays the last element of the scrollview.

like image 407
Mads Lee Jensen Avatar asked Apr 25 '12 19:04

Mads Lee Jensen


1 Answers

I found a solution for my problem, there might very well be a better solution but this actually works quite well :)

Before i start animating i see if the contentSize.height is larger than the target height, if so i do following:

if (mMessages.contentSize.height > 150.f)
{
    CGFloat expandedOffsetY = 52.f;
    CGFloat collapsedBottomOffset = (150.f + 18.f);
    CGFloat expandedBottomOffset = (MIN(320.f, mMessages.contentSize.height) + expandedOffsetY);
    tableFrame.origin.y = (collapsedBottomOffset - expandedBottomOffset) + expandedOffsetY;
}
else
{   
    tableFrame.origin.y = 18.f;
    tableFrame.size.height = 150.f;
}

this will put the tableview in a minus origin.y, then i have wrapped the tableview inside a "parent" uiview with clip sub views = YES.

Then i have a "completed" animation block that "resets" to the target values.

CGRect tableFrame = mMessages.frame;
tableFrame.origin.y = 18.f;
tableFrame.size.height = 150.f;
mMessages.frame = tableFrame;

if (mMessages.contentSize.height > tableFrame.size.height)
{
    float contentOffsetY = mMessages.contentSize.height - tableFrame.size.height;
    mMessages.contentOffset = CGPointMake(0.0f, contentOffsetY);
}
like image 194
Mads Lee Jensen Avatar answered Oct 21 '22 10:10

Mads Lee Jensen