Search code examples
iosobjective-cuitableviewsearchbar

Search Bar does not receive touches


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;
}

Solution

  • 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.