天天看點

UITableView 設定sectionHeader 懸浮的位置

最近開發遇到一個挺糾結的需求,沖突點在于我需要控制懸浮的位置,多方查驗,找到了解決方案

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

//sectionHeaderHeight 是sectionHeader的高度
    CGFloat height = 100;//height是正的就是向下偏移 正負代表了方向
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);//有的介紹是 UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);但是我實驗在項目中重新整理會有點問題
    }
    else if (scrollView.contentOffset.y >= sectionHeaderHeight)
    {
 //當視圖滑動的距離大于header時,這裡就可以設定section1的header的位置啦 其實這個位置可以更大一些 隻要你能确定頂部的高度
        scrollView.contentInset = UIEdgeInsetsMake(sectionHeaderHeight+height, 0, 0, 0);
    }
}