既可以让headerView不悬浮在顶部,也可以让footerView不停留在底部。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;
CGFloat sectionFooterHeight = 10;
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
}else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
}else if (offsetY >= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
{
scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight), 0);
}
}
本文介绍了一种在iOS应用中通过调整scrollView的内容偏移量,实现HeaderView不下沉及FooterView不上浮效果的方法。该方法利用了scrollViewDidScroll代理方法,并通过计算不同滚动位置时的contentInset值来实现。

1万+

被折叠的 条评论
为什么被折叠?



