3

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

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Muhammad Umair
  • 583
  • 11
  • 32

2 Answers2

1

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){
                  }
    ];
            }
}
maddy
  • 4,001
  • 8
  • 42
  • 65
0

try this code

- (void)scrollViewDidScroll:(UIScrollView *)scrollView  
{  
    CGFloat sectionHeaderHeight = 40;  
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {  
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);  
    }  
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {  
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);  
    }  
} 

but your problem is,your tableview header is not 40,it's 35 or 25,that means your tableview can't setup a correct number to the inset,try change 40 to correct header height.

C.Chen
  • 1
  • 1