1

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 relevant 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

Imran
  • 12,950
  • 8
  • 64
  • 79

3 Answers3

2

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];
  }
}
strange
  • 9,654
  • 6
  • 33
  • 47
1

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.titleViewof the ViewController. This offers some nice delegate methods.

Felix Lamouroux
  • 7,414
  • 29
  • 46
  • Thanks for the tip on using a UISearchBar. I will try that. My problem is that I'm not sure how to detect when the user clicks the mapview, so I have nowhere to write that line of code :) – Imran Jan 30 '10 at 17:44
  • I don't think you can do this! See: http://stackoverflow.com/questions/1049889/how-to-intercept-touches-events-on-a-mkmapview-or-uiwebview-objects. Let me know if I am misunderstanding. I am looking into overlaying a transparent view over the map when the keyboard is up and getting touches from that (the accepted solution in my link looks overly complex for my purposes). – Imran Jan 30 '10 at 18:50
  • You seem to be right. A transparent view seems to be a good idea. You should probably also forward all touch events from that view to the mapview. (I removed my wrong code) – Felix Lamouroux Jan 30 '10 at 19:07
  • OK, great. Things are coming into focus now. Thanks for all the help. – Imran Jan 30 '10 at 19:31
1

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];
}
Rog
  • 18,602
  • 6
  • 76
  • 97