I have a UITextView in my Navbar that is acting as a search box. I would like to dismiss the associated keyboard when the user taps below the text box - namely on the MKMapView. However I can't figure out how to do this since it doesn't look like I can intercept touches from the mapview.
I have looked at a number of solutions, but none seem to work for my case as far as I can tell. Does anyone have a simple way to do this? I am a bit of a noob, so please let me know if I am not providing some releva开发者_开发问答nt information, and please provide a few lines of example code in your answer if you can - I am still a bit shaky with terminology. Thanks!
screenshot http://img532.imageshack.us/img532/4070/keyboardsm.png
Assuming
UITextView *textView;
Then you can dismiss the keyBoard by sending:
[textView resignFirstResponder];
Also: you may prefer to use a UISearchBar
and set it as the navigationItem.titleView
of the ViewController. This offers some nice delegate methods.
I know this is an old one but in case anyone else is looks for a simple answer to this, here's my solution to get a "mapView" to resignFirstResponder. This will work in a similar way to the Google Maps app on the iphone, where a semi transparent box appears when you start editing the search text field.
Firstly you need to make your view controller a UISearchBarDelegate
@interface ViewControllerName <UISearchBarDelegate>
// your code here
@end
Then implement the following delegate methods:
@implementation ViewControllerName
// your code here
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
darkBg = [[UIControl alloc] initWithFrame:CGRectMake(0, 244, 320, 300)];
[darkBg setBackgroundColor:[UIColor blackColor]];
[darkBg addTarget:nil action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchDown];
[darkBg setAlpha:0.8];
[UIView beginAnimations:@"slideup" context:nil];
[darkBg setCenter:CGPointMake(160, 194)];
[self.view addSubview:darkBg];
[UIView commitAnimations];
}
- (void)hideKeyboard {
[UIView beginAnimations:@"fadeou" context:nil];
[darkBg setAlpha:0.0];
[UIView commitAnimations];
[addressField resignFirstResponder];
[darkBg performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
[darkBg release];
}
A less hacky solution is to rely on one of the MKMapView delegate methods to dismiss the keyboard
- (void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
if ([self.textField isFirstResponder]) {
[self.textField resignFirstResponder];
}
}
精彩评论