How to animate UITableView header view

前端 未结 7 1421
旧巷少年郎
旧巷少年郎 2020-12-29 09:32

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 go

7条回答
  •  有刺的猬
    2020-12-29 10:00

    I found the definitive and proper way to achieve a real animation on tableHeaderViews!

    • First, if you're in Autolayout, keep the Margin 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 and endUpdates, but put endUpdate into the animation block.

    // [self.tableView setTableHeaderView:headerView]; // not needed if already there
    [self.tableView beginUpdates]; 
    CGRect headerFrame = self.tableView.tableHeaderView.bounds;
    headerFrame.size.height = PLAccountHeaderErrorHeight;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.tableView.tableHeaderView.bounds = headerFrame;
    
        [self.tableView endUpdates];
    }];
    

    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.

提交回复
热议问题