Search code examples
iosuisearchbaruipopovercontrolleruisearchdisplaycontrolleruikeyboard

UISearchDisplayController with UISearchBar in UIPopover dismiss keyboard issue


I have a UIPopoverController with UITableViewController in it. Also, I am using UISearchDisplayController with it. My class interface is like this:

@interface SearchController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate> {

UISearchBar *_searchBar;
UISearchDisplayController *_searchDisplayVC;
}

My init looks like this:

//create a search bar
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar.delegate = self;

_searchDisplayVC = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];

self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

// add a searchbar
self.tableView.tableHeaderView = _searchBar;

My issue is that when I select search bar to type search phrase in it, the keyboard does not dismiss even if trying this:

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

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    NSLog(@"Dismiss popover controller");
    [_searchBar becomeFirstResponder];
    [_searchBar resignFirstResponder];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    //trying to resolve the issue with not working automatic hiding of a keyboard.
    [searchBar resignFirstResponder];
    [searchBar endEditing:YES];
}

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

-(BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

+ (void)dismissKeyboard {
    [[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
}

Anyone knows how to dismiss the keyboard?


Solution

  • I have found a resolution to this issue. In parent view controller I have overrided the UIViewController method named disablesAutomaticKeyboardDismissal to that:

    -(BOOL)disablesAutomaticKeyboardDismissal {
        return NO;
    }
    

    And It works fine! :) The keyboard is hiding as expected. Thanks @ujell for interesting in to this issue:)