第一个cell遮挡部分tableHeaderView

iOS tableview中第一个cell半遮挡tableHeaderView

有时候会有这种类似的需求
QQ20210309-113420

图中,第一个cell半遮挡住了tableHeaderView
底部的白色区域就是第一个cell,上面蓝色带搜索框的区域就是tableHeaderView

实现

假设已知条件:

  • cell遮挡部分的高度为 20pt
  • headerView整体高度为120pt
  • 也就是可见高度为100pt
1. 设置一个tableHeaderView,高度为可见高度

    UIView *placeHolderHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    
    self.tableView.tableHeaderView = placeHolderHeaderView;
    
2. 添加需要显示的headerView

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
    
    [self.tableView addSubview:headerView];
    
3. 设置headerView显示一直在页面最底层
    headerView.layer.zPosition = -1;
4. 移除占位的headerView
    [placeHolderHeaderView removeFromSuperview];

必须移除,不然header无法响应事件

5. 注意
    // 现在这样就可以看到,第一个cell,遮挡住了tableHeaderView的底部20pt的空间
    // 但是,第一个cell,顶部20pt的空间无法响应事件,我还没找到解决办法

如果有别的方式实现,或者解决这20pt无法响应的方法,欢迎告知

posted @ 2021-03-09 11:54  赎罪的码农  阅读(454)  评论(0)    收藏  举报