plain类型的tableView的section header是自动吸顶到tableView 的顶部的。有时候可能需要改变它的吸顶位置,我
们通过scrollview的 contentInset 属性来实现。contentInset是用来确定scrollview的contenview在scrollview上显示的范围的一个属性
代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {// any offset changes
// change the position of section header when scrolled by changing scroll's contentInset
CGFloat sectionHeaderHeight = 50;
CGFloat ceilPositon = CGRectGetHeight(_headerView.frame)-sectionHeaderHeight;
if (scrollView.contentOffset.y < sectionHeaderHeight){
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
// the "scrollView.contentOffset.y" must larger than its position to ceiling
if (scrollView.contentOffset.y>=ceilPositon && scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(sectionHeaderHeight, 0, 0, 0);
}
}
ceilPositon是我需要的section header的吸顶位置,当tableView的偏移量小于sectionHeaderHeight的时候,正常显示;当当tableView的偏移量大于ceilPositon,也就是我们期望的吸顶位置的时候,我们就把scrollView.contentInset的top置为sectionHeaderHeight的高度,于是就实现了我们想要的效果,可以随意更改section
header的吸顶位置。同样,此方法也可以使plain类型的tableview失去粘性效果,只需要改变scrollView.contentInset即可.
自定义UITableView Section Header 吸顶位置
在UITableView中,通常section header会自动吸附到顶部。本文介绍如何通过调整`contentInset`属性来自定义其吸顶位置。通过监听`scrollViewDidScroll`方法,当滚动偏移量达到设定值时改变contentInset,实现header在特定位置吸附,从而灵活控制section header的显示效果。
191

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



