Search code examples
iosobjective-cios7uisearchbaruisearchdisplaycontroller

UISearchDisplayController search bar will not dismiss keyboard?


I have a table view, being used in conjunction with a UISearchDisplayController which incorporates a UISearchBar into the header of the table view. I am unable to dismiss the keyboard when the user taps 'Search' on the keyboard. I have done the following in my view controller:

- (void)viewDidLoad
{

    [super viewDidLoad];

    [self setTitle:[NSString stringWithFormat:NSLocalizedString(@"ViewTitle", nil), _hotspots.count]];

    // Set up view options
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];

    }

    [_tableView setTableFooterView:[[UIView alloc] init]];


    // Fix for the issue where search bar would display incorrectly on iOS 7 modal iPad view
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {

        self.edgesForExtendedLayout = UIRectEdgeNone;

    }


    // Set localisable strings for interface elements
    [self.searchDisplayController.searchBar setPlaceholder:NSLocalizedString(@"PlaceholderText", nil)];


    // Hide Search Bar
    CGRect newBounds = _tableView.bounds;
    newBounds.origin.y = newBounds.origin.y + self.searchDisplayController.searchBar.bounds.size.height;
    _tableView.bounds = newBounds;


    // Set our search bar delegate methods
    [self.searchDisplayController.searchBar setDelegate:self];

}

I have also implemented the relevant delegate methods for the UISearchBar...

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return YES;
}

When I tap 'Search' on the keyboard, the search bar does indeed resign first responder status, but the keyboard is not dismissed. I can still continue to type text, but it's not entered into my text bar, and tapping the 'Search' button again does not trigger the above delegate method.

I have also tried replacing [searchBar resignFirstResponder] with [self.view endEditing:YES] which has had no effect.

The strange thing is, this is behaving differently on iOS 6 to iOS 7... On iOS 6 the search button will never dismiss the search bae correctly, however on iOS 7, if i select an item within the searchDisplayController's tableview, which pushes a new view, when i return back to the view containing the searchDisplayController, the 'Search' button dismisses the keyboard correctly each time.

Can anyone enlighten me as to what is going on here?

UPDATE:

Additional information, this is a very simple view controller, it has a table view, a data source, and the search display controller code. I am displaying it within a modal view (form sheet) on an iPad and the interface is contained within a storyboard.

UPDATE 2:

To clarify, this issue is only happening on the iPad (both iOS 6 & 7) when the view is shown in a modal (form sheet' view, the keyboard is dismissed fine on an iPhone.


Solution

  • Use this magic method. I use it all the time, the good thing is that it always work for any field, no matter where (since it's in the main windows) and which.

    + (void)dismissKeyboard {
        [[YOURAPPDELEGATE window] endEditing:YES];
    }
    

    EDIT :

    Ok, got it with "Update 2" of your question. It is a behavior with form sheet not something with search field.

    look at this link : Modal Dialog Does Not Dismiss Keyboard