Search code examples
iosobjective-cuitableviewuisearchbardisplaycontrol

UITableview header not shown again if scrolled up


I have UITableview Controller, who has search bar in header. When i scrolled up the table view and it bounce. But the search bar hides. when i scroll down then the search bar shown. can any one tell me how can i show the search bar again?

I have tried the following code but it doesn't work smoothly:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        //scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);
        [self.tableView setContentOffset:newOffset animated:YES];
    }
}

Here is screen shot for view before scrolling: enter image description here

And this is wrong view, After scrolling:

enter image description here


Solution

  • How about disabling animation to off and put custom animation like this:

    also make your sure to have same sectionHeader height using MAcro. i have still doubt in your one of comment saying 35 or 25.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
                CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
                if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
    
        [UIView animateWithDuration: 1.0
                          animations: ^{
                              scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
                          }completion: ^(BOOL finished){
                          }
            ];
                } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
                    CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);
    
    
    [UIView animateWithDuration: 1.0
                      animations: ^{
                          [self.tableView setContentOffset:newOffset animated:NO];
                      }completion: ^(BOOL finished){
                      }
        ];
                }
    }