Show / hide UITableView (UITextField) header just like Mail application

I would like to show and hide the UITableView (UITextField) header in the same way that the standard Mail application does a UISearchBar with it, but could not figure out how to implement the behavior.

The behavior that is relevant to me is as follows:

  • On the first display, the title (search bar) is hidden.
  • When the user scrolls down, the title is displayed.
  • Even when the contents of the table (row) do not fill the entire view of the table, the title can be scrolled back from the field of view (very important).
  • When returning to the same view, the position of the heading exactly coincides with where it was left (completely or completely out of sight, or somewhere in the middle).

The key can scroll the title back from the field of view, even if the contents of the table (row) does not fill the entire view.

I tried setting the contentOffset, and although I can hide the title for the first view by doing this, the title cannot be scrolled back from view when the contents of the table (row) does not fill the entire view of the table with this method only.

Can anyone advise here?

+3
source share
1 answer

Have you ever received what you were looking for? I have this in some code some time ago that does what you ask for. Shows the search bar in the header when removing 40px. Hides under the navigation bar when the table scrolls 40 pixels.

search is the iVar flag that I set when the search bar is active.

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat amountOfDrag = scrollView.contentOffset.y;
    UIEdgeInsets edgeAtEntry = self.tableView.contentInset;
    UIEdgeInsets edgeSetNew;
    CGFloat underlay = self.topLayoutGuide.length;
    CGFloat searchBarViewHeight = 44.0f;
    BOOL needsReset = NO;
    if (amountOfDrag <= -40 && edgeAtEntry.top == underlay - searchBarViewHeight) {
        needsReset = YES;
        edgeSetNew = UIEdgeInsetsMake(0.0f + underlay, 0.0f, self.bottomLayoutGuide.length, 0.0f);
    } else if (amountOfDrag >= 40 && edgeAtEntry.top != underlay - searchBarViewHeight) {
        needsReset = YES;
        edgeSetNew = UIEdgeInsetsMake(-searchBarViewHeight + underlay, 0.0f, self.bottomLayoutGuide.length, 0.0f); //hides searchbar under navbar
    }

    if (needsReset && !searching) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        scrollView.contentInset = edgeSetNew;
        [UIView commitAnimations];
    }
}
0

All Articles