2

I followed this post to make my section header animate out of view when the tableview is scrolled. However, when scrolling back to the top, the header does not come back into view. Ever. Given that I followed the solution exactly (and I'm not setting the contentInset anywhere else), I'm quite perplexed. Can anyone point me in the right direction? Here is my code:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  CGPoint p = scrollView.contentOffset;

  CGFloat height = [self tableView:self.agendaDetailTable heightForHeaderInSection:0];

  if (p.y <= height && p.y > 0) {
    self.agendaDetailTable.contentInset = UIEdgeInsetsMake(-p.y, 0, 0, 0);
  } else if (p.y >= height) {
    self.agendaDetailTable.contentInset = UIEdgeInsetsMake(-height, 0, 0, 0);
  }
}
Community
  • 1
  • 1
Jacob
  • 926
  • 1
  • 14
  • 25
  • The snippet works perfectly for me both for plane and grouped table. Try to log your `p.y` and `height`. – A-Live Mar 20 '12 at 21:22
  • The header in the first section comes back into view for you with this code? Mine stops immediately short of the header with the entire first tableview cell visible. I logged the p.y and height values. Height remains 134 and p.y ranges from 0 to the maximum contentOffset, but when scrolling back to the top, it stops at 134. – Jacob Mar 20 '12 at 22:38
  • Also, when I scroll down and the header view is partially offscreen, I'm unable to scroll back to the top, if that helps at all. – Jacob Mar 20 '12 at 22:44
  • ah i see, you should understand the code works for the case when table `alwaysBounceVertical` and `bounces` are enabled. – A-Live Mar 21 '12 at 01:14
  • While this solution works, unfortunately, implications from client requirements dictate that bounces must be set to NO :( Oh well. Back to the drawing board. Thanks for your answer! I gave it a checkmark even though I can't use it. – Jacob Mar 22 '12 at 15:31
  • Did you ever sort out this problem? I too am trying to achieve the same effect and it's really bugging me now! – Adam Carter Aug 07 '12 at 15:41

2 Answers2

4

Enable bounces and alwaysBounceVertical for the table to allow it to scroll over the edge of content and rewrite the offset while that's happening.

A-Live
  • 8,904
  • 2
  • 39
  • 74
1

Jacob's solution works well. One issue is that the content is off by one pixel after scroll. I have a header with a black border and can see the problem after I scroll and then return to the top. By changing p.y > 0 to p.y >= 0 my header when scrolled back to zero is restored properly.

/* to fix floating headers */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint p = scrollView.contentOffset;

    CGFloat height = (float) DASHBOARD_SUMMARY_PANEL_HEADER_HEIGHT;

    if (p.y <= height && p.y >= 0) {
        self.SummaryPanelTableView.contentInset = UIEdgeInsetsMake(-p.y, 0, 0, 0);
    } else if (p.y >= height) {
        self.SummaryPanelTableView.contentInset = UIEdgeInsetsMake(-height, 0, 0, 0);
    }
}
bret
  • 796
  • 9
  • 5