Search code examples
iphoneiphone-softkeyboard

Help hiding keyboard when searchdisplaycontroller is active


I added a tapgesturerecognizer to my view so when the user hits the search bar, but then decides to not type, the user can tap on the screen behind the keyboard, and the keyboard will disappear. this is my code in viewdidload:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

Then to handle the tap:

- (void)handleTap:(UITapGestureRecognizer *)gesture {
    if (self.searchDisplayController.active == YES) {
        [self.searchDisplayController setActive:NO];
    }
}

However, now my other buttons like items in the tabbar are not available. Am I missing something else? Thanks!


Solution

  • Try

    [self.searchDisplayController.searchBar resignFirstResponder];
    

    This will hide the keyboard without deactivating search mode.