How to resize a tableHeaderView of a UITableView?

后端 未结 19 2246
死守一世寂寞
死守一世寂寞 2020-11-29 16:14

I\'m having trouble resizing a tableHeaderView. It simple doesn\'t work.

1) Create a UITableView and UIView (100 x 320 px);

2) Set the UIView as tableHeaderV

19条回答
  •  不知归路
    2020-11-29 17:01

    If you want to conditionally animate the changes you can do the following:

    - (void) showHeader:(BOOL)show animated:(BOOL)animated{
    
        CGRect closedFrame = CGRectMake(0, 0, self.view.frame.size.width, 0);
        CGRect newFrame = show?self.initialFrame:closedFrame;
    
        if(animated){
            // The UIView animation block handles the animation of our header view
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.3];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
            // beginUpdates and endUpdates trigger the animation of our cells
            [self.tableView beginUpdates];
        }
    
        self.headerView.frame = newFrame;
        [self.tableView setTableHeaderView:self.headerView];
    
        if(animated){
            [self.tableView endUpdates];
            [UIView commitAnimations];
        }
    }
    

    Please note that the animation is two-folded:

    1. The animation of the cells below the tableHeaderView. This is done using beginUpdates and endUpdates
    2. The animation of the actual header view. This is done using a UIView animation block.

    In order to synchronize those two animations the animationCurve has to be set to UIViewAnimationCurveEaseInOut and the duration to 0.3, which seems to be what the UITableView uses for it's animation.

    Update

    I created an Xcode project on gihub, which does this. Check out the project ResizeTableHeaderViewAnimated in besi/ios-quickies

    screenshot

提交回复
热议问题