Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Bar Cancel Button don´t work in ios 7 sometimes

Cancel button in search bar don´t work in iOS 7 when search bar is initially hidden.

I follow this tutorial to create a search bar in tableview:

raywenderlich tutorial

There are a example project in this tutorial, is better use this project than my explanation :)

In iOS 5 and 6 works fine. I have reviewed all delegates.

There are two possibilities. The first is to press the button when the bar is hidden, the second is to press the button when the bar is displayed (moving the table down with a gesture you can see the search bar)

If search bar is hidden initially cancel button don´t work, it don't call calcel delegate method:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

Sorry I can not explain it better.

Thanks

like image 954
mjimcua Avatar asked Oct 08 '13 10:10

mjimcua


1 Answers

I googled all over internet and couldn't find a solution. so i changed the UItableview behaviour.

instead of [searchBar becomeFirstResponder]; I scroll down the tableview.

- (IBAction)goToSearch:(id)sender {

 scroll down to show the table.
//    CGRect newBounds = self.tableView.bounds;
//    newBounds.origin.y =0;
//    
//    self.tableView.bounds = newBounds;
//[searchBar becomeFirstResponder];

    CGPoint contentOffset=self.tableView.contentOffset;
    contentOffset.y=0;
    [self.tableView setContentOffset:contentOffset animated:YES];


}

in my ViewDidload:

//        CGRect newBounds = self.tableView.bounds;
//        newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
        // self.tableView.bounds = newBounds;

        CGPoint contentOffset=self.tableView.contentOffset;
        contentOffset.y=self.tableView.bounds.origin.y + searchBar.bounds.size.height;
        self.tableView.contentOffset=contentOffset;

If found for some reasons, in iOS 7 , change table view bounds cause search bar to disappear. Hope that helps.

like image 140
Kiddo Avatar answered Oct 11 '22 14:10

Kiddo