ios的tableview中sectionHeaderView会随着滑动黏在上方,直到新的sectionHeaderView出现并替换掉,这是个好的特性,但是在为了实现PM某些需求的时候,又不是很符合心意,在网上查了下,找到了其解决方法:
tableView 格式是 plain 的时候有粘性,需要解决,格式是 group 的时候没有粘性。
// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 这个高度写headerView的高度
CGFloat sectionHeaderHeight = 40;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
}
else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
swift写法:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let sectionHeaderHeight = 45
if (Int(scrollView.contentOffset.y) <= sectionHeaderHeight&&scrollView.contentOffset.y >= 0) {
scrollView.contentInset = UIEdgeInsets(top: -scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
}
else if (Int(scrollView.contentOffset.y) >= sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsets(top: CGFloat(-sectionHeaderHeight), left: 0, bottom: 0, right: 0)
}
}
header \ footer 都需要解决粘性的时候:
Header\footer都需要解决的时候
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
UITableView *tableview = (UITableView *)scrollView;
CGFloat sectionHeaderHeight = 64;//头
CGFloat sectionFooterHeight = self.type == OrderOrGoodsGoods?45:85;//脚
CGFloat offsetY = tableview.contentOffset.y;
if (offsetY >= 0 && offsetY <= sectionHeaderHeight){
tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
}else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight){
tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
}else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height){
tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
}
}
网友评论