0

the search bar does not receive touches when scrolled but does receive when it is on the original contentOffSetI have a search bar implemented and it 'sticks' to the tableView. Now, after the tableView starts scrolling, the searchBar does not receive touches and become the first responder, instead, the cell behind it does and navigates to the next view. What can I do to make the searchBar receive touches?

Code for keeping it static:

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

    UISearchBar *searchBar = self.search;
    [[self.tableView.tableHeaderView superview] bringSubviewToFront:self.tableView.tableHeaderView];
    CGRect searchBarFrame = searchBar.frame;
    if (self.inSearchMode)
    {

        searchBarFrame.origin.y = 0;
    }
    else
    {

        searchBarFrame.origin.y = MAX(0, scrollView.contentOffset.y + scrollView.contentInset.top);
    }

    self.search.frame = searchBarFrame;
}
sam24
  • 99
  • 2
  • 10
  • 1
    can you post screenshot ? one moorething remember When the UISearchBar is the tableHeaderView, direct manipulation of it's frame in -scrollViewDidScroll: works on iOS 5.x but not on iOS 6. To get it working, you need to wrap the search bar in a plain UIView and add that as the table header. – Badal Shah Aug 05 '15 at 07:16
  • I have wrapped it in a UiView which is then set as the tableHeaderView – sam24 Aug 05 '15 at 07:20
  • 1
    try to set it once in place of your code . `UISearchBar *searchBar = searchDisplayController.searchBar; CGRect rect = searchBar.frame; rect.origin.y = MIN(0, scrollView.contentOffset.y); searchBar.frame = rect;` – Badal Shah Aug 05 '15 at 07:23
  • Tried it. the searchBar now disappears while scrolling – sam24 Aug 05 '15 at 07:27
  • 1
    yo need to also impliment search display controllers delegate method . - `(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { } -(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { }` – Badal Shah Aug 05 '15 at 07:28

1 Answers1

1

If you think about it, the table header frame will be outside the table bounds when you scroll. Try to add it as a subview to the tableView directly and set its frame relative to tableView bounds to keep it sticky.

Lucian Boboc
  • 480
  • 2
  • 6
  • @sam24 would you mind posting some code here? Having the same problem, still struggling. Thanks. – Anthony Oct 07 '15 at 16:41
  • If you're using tableview.frame to tableview.bounds. That should do the trick. That's all I did. – sam24 Oct 09 '15 at 12:22