I need to animate the insertion of a tableview header view. I want that the table rows to slide down while the header view expands its frame.
So far the best result I got is by using the following code:
[self.tableView beginUpdates];
[self.tableView setTableHeaderView:self.someHeaderView];
[self.tableView endUpdates];
The problem with this solution is that the header itself doesn't get animated, its frame doesn't expand.
So the effect I'm getting is that the table rows slide down (as I want) but the header view is immediately shown, and I want it to expand it's frame with animation.
Any ideas?
Thanks.
SWIFT 4 SOLUTION w/ Different Header
If you want this kind of animation, that is the solution that I have used: (even though in my case I had also to play a bit with the opacity of the objects, since I was loading another completely different header)
So, basically, it's all about changing the frame inside the
.animate
closure. The beging/end updates is needed in order to move and update the other elements of the tableView.have the header frame at
CGRectZero
and set its frame using animationHere's what I got to work in Swift, with an initial frame height of zero and updating it to its full height in the animations closure:
I found the definitive and proper way to achieve a real animation on
tableHeaderView
s!• First, if you're in
Autolayout
, keep theMargin
system inside your header view. With Xcode 8 its now possible (at last!), just do not set any constraint to any of the header subviews. You'll have to set the correct margins thought.• Then use
beginUpdates
andendUpdates
, but putendUpdate
into the animation block.Note: I only tried in iOS10, i'll post an update when my iOS9 simulator will be downloaded.
EDIT
Unfortunately, it doesn't work in iOS9 as well as iOS10: the header itself does not change its height, but header subviews seem to move as if the header bounds changed.
The selected answer didn't work for me. Had to do the following:
The really peculiar part is that this works in one view but not in a subclass of the same view controller / view hierarchy.
Use below code this works