tableView在穿透导航栏以后让section悬浮在导航栏下面

上一篇我提到了让背景图片穿透导航栏,对于tableView来说,他的section会随着tableview的滑动一起滑动,直到当section将要越出在tableView的contentInsets的top时(也就是tableView的内容距离顶部的距离,默认是0,但是如果设置了tableHeaderView,系统会帮我们自动调整为64,这里就会有一个问题,在导航栏透明以及tableView没有滑动时,会有64的留白,所以我们需要设置 self.automaticallyAdjustsScrollViewInsets = NO;,不让系统调整,我们通过监听tableView的滚动自己调整),他就会悬浮在那里,但是当tableView穿透了导航栏以后tableView的farme默认是从0开始计算的,所以section的悬浮就会被NavigationBar给挡住,解决方案如下:
因为tableView继承于UIScrollView,scrollerView中我们用-(void)scrollViewDidScroll:(UIScrollView *)scrollView这个方法来监听scrollerView的时刻滚动,那么我们也可以在这个方法里监听tableView的时刻滚动,当tableView的y偏移小于tableView.tableHeaderView的高度减去64以及tableView的y偏移大于0,设置tableView的内容距离顶部的距离为0,当
tableView的y偏移大于tableView.tableHeaderView的高度-64,也就是section将要穿透navigationBar的时候,设置self.tableView的内容距离顶部的高度为64,这样tableView的内容就会从section就会悬浮在导航栏下面

//LXTableHeaderViewHeight是tableViewHeaderView的高度,注意在设置tableView的tableHeaderView时,虽然在tableView在穿透导航栏的情况下,纵坐标从0开始计算,但是对于tableView的headerView来说,系统还是会自动偏移出64(导航栏44+状态栏20)的高度,所以设置    self.automaticallyAdjustsScrollViewInsets = NO;,这样系统就不会帮我们调整了
#pragma mark - UIScrollerViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     //获取tableView当前的y偏移
    CGFloat contentOffsety  = scrollView.contentOffset.y;
    //如果当前的section还没有超出navigationBar,那么就是默认的tableView的contentInset
    if (contentOffsety<=(LXTableHeaderViewHeight-64)&&contentOffsety>=0) {
                self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    //当section将要就如navigationBar的后面时,改变tableView的contentInset的top,那么section的悬浮位置就会改变
    else if(contentOffsety>=LXTableHeaderViewHeight-64){
                self.tableView.contentInset  = UIEdgeInsetsMake(64, 0, 0, 0);
    }

    if (contentOffsety>60) {
         self.testView.backgroundColor =[UIColor colorWithRed:red green:green blue:blue alpha:scale];
    }
    else{
        [self.testView removeFromSuperview];
        _testView = nil;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在每个表格行的最后一列添加一个自定义的单元格,并在该单元格中放置一个按钮。具体步骤如下: 1. 创建一个带有按钮的自定义单元格类。 ```swift class ButtonTableViewCell: UITableViewCell { let button = UIButton(type: .system) override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) button.setTitle("Button", for: .normal) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) contentView.addSubview(button) button.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ button.topAnchor.constraint(equalTo: contentView.topAnchor), button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), button.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), button.widthAnchor.constraint(equalToConstant: 80) ]) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc func buttonTapped() { // Handle button tap event } } ``` 2. 注册该自定义单元格类。 ```swift tableView.register(ButtonTableViewCell.self, forCellReuseIdentifier: "ButtonCell") ``` 3. 在 `cellForRowAt` 方法中使用该自定义单元格类并返回。 ```swift func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath) as! ButtonTableViewCell // Configure the cell return cell } ``` 这样每行最后一列就会显示一个按钮。你可以根据需要对按钮进行自定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值