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里的数值就可以了。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...