UISearchBar's Cancel and Clear Buttons Not Working in iOS 7

12,685

Solution 1

I have the same problem, I tried with the following code. Please try this one.

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    controller.active = YES;

    [self.view addSubview:controller.searchBar];
    [self.view bringSubviewToFront:controller.searchBar];
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {

    tableView.frame = self.archiveListTblView.frame;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    controller.active=NO;

    [self.view addSubview:controller.searchBar];
}

Solution 2

This problem seems to come from the new behaviour of the translucent property in a navigation bar.

Since iOS 7 navigation bars are translucent by default. And it looks like it's overlapping the search bar when you display it after pressing a button. If you scroll to the top of your listView and use the search bar, it should be working correctly.

Try to set in your controller:

float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion >= 7.0)
{
    self.navigationController.navigationBar.translucent = NO;
}

This should quickly solve the problem.

But I think for a better fix you should see the iOS 7 transition guide where they explain how to handle translucent navigation bars.

Hope that helps.

Solution 3

button Cancel and button Clear won't work if you touch button Search on navigation. If you tap on Search bar to star searching, it works normally => That mean if search bar is not visible on screen before staring search, then those buttons seem like disable too.

So that, I found a solution, but it is not totally perfect. Before you make search bar become first response, you have to scroll table view to top. Try this code.

-(IBAction)goToSearch:(id)sender {
     // Make search bar visible on screen before make it response
    [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];

    // Make search bar active
    [candySearchBar becomeFirstResponder];
}

The only issue occur if you do this, is your table view will scroll to top, which mean when you cancel search, you lost current cell index before.

Solution 4

I have had this problem too. Strangely other delegate methods of UISearchBarDelegate are being called. A workaround might be:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchText length] == 0) {
        NSLog("No Text");
    }
}

It worked for me

Share:
12,685
Mike
Author by

Mike

Updated on June 17, 2022

Comments

  • Mike
    Mike about 2 years

    I have an xCode project that contains a tableview with a “Search Bar and Search Display Controller” to allow the user to refine the list of displayed items. In general, the guidance provided in http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view was followed. I recently downloaded the latest xCode (Version 5.0 (5A1413)) with iOS 7 support and have been testing the app in question on different targets.

    When running this app on an iOS 6 target (emulator or real device), it works as expected meaning that pressing the cancel button removes the search bar and pressing the clear button (little gray x) clears all of the search criteria already typed in by the user. But when the project is run on an iOS 7 target both the clear and cancel button do not work.

    The searchBarCancelButtonClicked method is implemented in this project, and I have verified that it is not called when the target is running iOS 7.

    - (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar
    {
        NSLog(@"searchBarCancelButtonClicked called");
    
        self.searchBar.text = nil;
    
        …
    
        // Hide Search bar when cancelled
        [self hideSeachBar];
    
        [self.searchBar resignFirstResponder];
    
        …
        }
    

    My table view controller is setup to be the UISearchDisplayDelegate and UISearchBarDelegate. And it appears that this is still working as searchBar:textDidChange: is called on either a iOS 6 or 7 target.

    @interface ItemViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
    …
    @end
    

    I cannot see any other posts related to this or any iOS 7 change material (like https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1) that mentions any recoding that needs to be done to support iOS7.

    Any thoughts on this? Thanks

  • Mike
    Mike over 10 years
    Curious how your self.archiveListTblView is set and used.
  • Prabakaran
    Prabakaran over 10 years
    Hello Mike, In my point of view : iOS7 is automatically makes the layout for its nativity windows. That is the reason for the problem which we have faced. And one more thing, I have tried with "self.view.frame" instead of "self.archiveListTblView.frame". Even this code too working fine for me. So, the problem was we didn't set the frame for tableview. :-P