方法一:直接设置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里的数值就可以了。
原文地址:https://www.cnblogs.com/runningGY/p/15236721.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。