表头下拉填充 MJRefresh 刷新被tableView的he

2019-10-15  本文已影响0人  网名起的不要像我的这样长

最近开发中遇到tableView 下拉需要一个纯色view 遮盖头部,
就像这样


WechatIMG4.jpeg

代码实现

// 创建一个tableView 的 headerView, headerView 上添加一个子视图, 并且高度都为0
- (UIView *)tabHeaderView
{
    if (!_tabHeaderView) {
        self.tabHeaderView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, KScreenWidth, 0))];
        UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, KScreenWidth, 0))];
        _headerView.backgroundColor =[UIColor colorWithHexString:@"#fdaba0"];
        _tabHeaderView.backgroundColor = [UIColor colorWithHexString:@"#fdaba0"];
        [self.tabHeaderView addSubview:_headerimageView];
    }
    return _tabHeaderView;
}

// 设置表头
_tableView.tableHeaderView = self.tabHeaderView;

// scrollowView 代理回调, 改变headerView 的高度
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{        
  CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        UITableView *tableView = ((UITableView *)scrollView);
        UIView *view = tableView.tableHeaderView;
        UIView *imageView = view.subviews[0];
        CGRect rect = CGRectMake(0, 0, KScreenWidth, 0);
        rect.origin.y = offsetY;
        rect.size.height = 0 - offsetY;
        imageView.frame = rect;
    }
}

以上代码实现了下拉纯色背景填充headerView 的功能
但是当该页面使用下拉刷新的功能的时候, 尴尬的事情发生了, headerView 将 MJRefresh 中的header盖住了...........
那么就需要更改下面的代码

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{        
  CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        UITableView *tableView = ((UITableView *)scrollView);
        UIView *view = tableView.tableHeaderView;
        UIView *imageView = view.subviews[0];
        CGRect rect = CGRectMake(0, 0, KScreenWidth, 0);
        rect.origin.y = offsetY;
        rect.size.height = 0 - offsetY;
        if (![view.subviews containsObject:tableView.mj_header]) {
            [view addSubview:tableView.mj_header];
        }
        imageView.frame = rect;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读