I'm trying to give my users a way to dismiss the keyboard, whether by clicking outside the keyboard or by having a DONE button on the keyboard itself.
I have created a Done button and it works fine on iOS 6:
UIToolbar *keyboardToolbar;
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 44, 320, 44)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"dismiss_keyboard", nil) style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyboard)];
NSArray *items = [NSArray arrayWithObjects:flexItem,doneItem, nil];
[keyboardToolbar setItems:items animated:YES];
for (UIView *subview in [searchBar subviews])
{
if( [subview isKindOfClass:[UITextField class]] )
{
((UITextField*)subview).delegate=self;
((UITextField*)subview).inputAccessoryView = keyboardToolbar;
break;
}
}
But on iOS 7 this button is no where to be found.
I also tried using the method where the user can click anywhere but the keyboard and make it disappear:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
//Code to dismiss keyboard.
}
But my view contains a UISearchBar
and a UITableView
but the touchesBegan
event doesn't fire when I touch those, only when I touch the parent UIView, which is not visible because it's covered by my UISearchBar
and my UITableView
. I have to touch the tiny space in between the two to fire the event.
How can I make my touchesBegan
method apply to any object on the screen? And why is my DONE button not showing up in iOS 7?