当UITableView在 style 为.plain的时候,手动设置的 sectionHeader和 sectionFooter会悬浮在页面直到当前 Section 消失。此时可以手动设置以下代码去除悬浮效果
在scrollViewDidScroll方法中
func scrollViewDidScroll(_scrollView:UIScrollView) {
letsectionHeaderHeight:CGFloat =50
letsectionFooterHeight:CGFloat=80
letoffsetY = scrollView.contentOffset.y
if(offsetY >=0&& offsetY <= sectionHeaderHeight)
{
scrollView.contentInset=UIEdgeInsets(top: -offsetY, left:0, bottom: -sectionFooterHeight, right:0)
//UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
}elseif(offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height- scrollView.frame.size.height- sectionFooterHeight)
{
scrollView.contentInset=UIEdgeInsets(top: -sectionHeaderHeight, left:0, bottom: -sectionFooterHeight, right:0)
//UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
}elseif(offsetY >= scrollView.contentSize.height- scrollView.frame.size.height- sectionFooterHeight && offsetY <= scrollView.contentSize.height- scrollView.frame.size.height)
{
scrollView.contentInset= UIEdgeInsets(top: -offsetY, left:0, bottom: -(scrollView.contentSize.height- scrollView.frame.size.height- sectionFooterHeight), right:0)
}
}