swift 关于让tableview的sectionHeaderView悬顶

 

方法一:直接设置tableview的style为plain风格,这种风格是自带该效果的

如果想要取消该效果,可以在代码里进行设置,因为UITableView继承自UIScrollView,所以可以直接在UIScrollerView的代理方法中实现,需要写在scrollViewDidScroll函数里

        if scrollView == PostTable {
            let tableSectionHeaderHeight = CGFloat(44)
             if scrollView.contentOffset.y <= tableSectionHeaderHeight && scrollView.contentOffset.y >= 0{
                  scrollView.contentInset = UIEdgeInsets(top: -scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
             }else if scrollView.contentOffset.y >= tableSectionHeaderHeight{
                  scrollView.contentInset = UIEdgeInsets(top: -tableSectionHeaderHeight, left: 0, bottom: 0, right: 0)
            }
        }

方法二:直接在代码里进行设置,因为UITableView继承自UIScrollView,所以可以直接在UIScrollerView的代理方法中实现

需要写在这个函数里面:

1 func scrollViewDidScroll(_ scrollView: UIScrollView) {
2 }

具体实现代码是:

 1         if scrollView == PostTable {
 2             let tableSectionHeaderHeight = CGFloat(44)
 3             if scrollView.contentOffset.y <= tableSectionHeaderHeight && scrollView.contentOffset.y >= 0{
 4                  scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: scrollView.contentOffset.y, right: 0)
 5             }else if scrollView.contentOffset.y >= tableSectionHeaderHeight{
 6                  scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tableSectionHeaderHeight, right: 0)
 7             }
 8         }

当然这个也可以设置sectionheader view一部分被隐藏一部分悬顶,修改contentInset里的数值就可以了。

posted @ 2021-09-07 09:13  僧侣行书  阅读(376)  评论(0编辑  收藏  举报