1

I have a searchbar in my application, but when I run it in simulator, it doesn't allow me to type in the search field.

In my code, the search bar is created in the xib file and then added as a subview of map view (programmatically) so that it lays on top of the map. Some of my code for my search bar:

[mapView addSubview: searchBar];
searchBar.placeholder = @"Search or Place Pin";
searchBar.searchBarStyle = UISearchBarStyleProminent;
searchBar.backgroundImage =[[UIImage alloc]init];
searchBar.barTintColor = [UIColor clearColor];
searchBar.delegate = self;

-(void) viewDidAppear:(BOOL)animated{
...
[searchBar becomeFirstResponder];
BOOL searchBarIsResponding = [searchBar becomeFirstResponder];
NSLog(@"first responder: %d", searchBarIsResponding);

....        
- (void) searchBarButtonClicked:(UISearchBar *)searchBar
{
[self searchCoordinatesForAddress:[searchBar text]];
[searchBar resignFirstResponder];
}

Edit: I tried setting mapView's userInteractionEnabled to NO to see whether it was an issue of mapView not passing its touches to searchBar, but I still couldn't type in searchBar, so I don't think that's the problem.

maddie
  • 1,854
  • 4
  • 30
  • 66
  • You are trying to add tap gesture on search bar which is not needed if you are using the delegate = self; Include UISearchBarDelegate and override its relevant methods. However, even if you want to tap gesture then your selector is wrong as you have assigned a selector without parameter but have used - (void) searchBarButtonClicked:(UISearchBar *)searchBar – SHN Jul 27 '16 at 15:56
  • So I originally did not have a tap gesture, and it still did not allow me to type into the search bar. If I remove the tap gesture and return the code to how it originally was, what do I need to change? What do you mean by override its relevant methods? – maddie Jul 27 '16 at 16:08

1 Answers1

0

If your mapView is a subview of another view, I would place the search bar as a subview of that view. Otherwise, have you checked if the searchBar is becoming first responder? It might be that the map view isn't passing touches to its subviews and thus the search bar isn't becoming first responder so you cant type. I hope this helps, I sadly cannot comment yet so I had to create this answer based on assumptions, please let me know if I misunderstood.

jniegsch
  • 380
  • 3
  • 13
  • Does this mean that my map view needs to have a tap gesture recognizer and then pass touches to search bar? because currently mapview does not have a tap gesture recognizer – maddie Jul 27 '16 at 19:44
  • If I understand the documentation correctly - no. When the mapView gets a tap it recognizes it and handles it accordingly - in this case it should tell the searchBar it was tapped. Also the current gesture recognized seems to override the display of the keyboard since when you tap it, you tell it to resign first responder again. I would suggest removing the gesture recognizer and seeing if you can type the text then. If not, add a function that check if the searchBar has become first responder. By the way are you implementing the 'UISearchBarDelegate' protocol? – jniegsch Jul 27 '16 at 19:52
  • So I deleted the search bar's tap gesture recognizer, but it still won't let me type. I will try adding a function to see if searchBar has become first responder. No, I'm not implementing UISearchBarDelegate protocol... – maddie Jul 27 '16 at 19:58
  • So I added the statements: [searchBar becomeFirstResponder]; BOOL searchBarIsResponding = [searchBar becomeFirstResponder]; NSLog(@"first responder: %d", searchBarIsResponding); And I get false -- why would it not become first responder though? – maddie Jul 27 '16 at 20:17
  • I have now moved the above statements (searchBar becomeFirstResponder ...) to viewDidAppear, and the print log returns true. However, I still cannot type in the text field? – maddie Jul 27 '16 at 20:21
  • You need to implement the protocol to tell the searchBar what it should do. Then you can access all the functions (if you want to handle the search yourself). – jniegsch Jul 27 '16 at 20:21
  • How are you adding the searchBar? Programmatically? If so could you post the code? – jniegsch Jul 27 '16 at 20:24
  • So I should set up code like posted in this question: http://stackoverflow.com/questions/31488884/how-to-implement-searchbar-and-its-delegate-method-in-ios ? – maddie Jul 27 '16 at 20:38
  • Not really, it seems the person is either storyboard initializing it or not adding it at all. Could you show me what you have done so far? – jniegsch Jul 27 '16 at 21:06
  • I updated my original post to include all my code relevant to searchBar :/ – maddie Jul 27 '16 at 21:26
  • BTW, I got the idea to add search bar as a subview from this post: http://stackoverflow.com/questions/14092857/how-can-i-overlay-a-search-bar-over-mapkit – maddie Jul 27 '16 at 22:36
  • Also, I tried setting mapView's userInteractionEnabled to NO to check whether it was an issue of mapView not passing its touches to searchbar .... it didn't help. I still couldn't type in the search bar – maddie Jul 27 '16 at 23:04
  • I'll play around with the code today and see if I can solve it :) – jniegsch Jul 28 '16 at 06:09
  • I got it to work using a basic storyboard, have you tried this? – jniegsch Jul 29 '16 at 07:31
  • - (void)viewDidLoad { [super viewDidLoad]; // Add SearchBar searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)]; [searchBar setDelegate:self]; [self.view addSubview:searchBar]; } – jniegsch Jul 29 '16 at 07:40
  • So by not making it a subview of the MKMapView seems to solve the issue – jniegsch Jul 29 '16 at 07:41
  • I couldn't get my search bar to sit on top of MapView without making it a subview (even when i dragged it under MapView's hierarchy in storyboard). Will this code allow it to sit directly on top of mapview? – maddie Jul 29 '16 at 15:15
  • Yeah this code will have the searchBar sitting on top of the MKMapView as long as the code is being called from a ViewController which places the searchBar on its view, which also contains the MKMapView. I hope this is clear :) – jniegsch Jul 29 '16 at 15:18
  • So I added your code and the searchbar doesn't show up in the simulator :? – maddie Jul 29 '16 at 17:03
  • What are you writing the class are you writing this in (aka. Subclasses from UIView, UIViewController, etc.)? – jniegsch Jul 29 '16 at 18:32
  • I'm writing this in my own class, filterPageViewController -- I'm sorry if this doesn't answer your question – maddie Jul 29 '16 at 19:03
  • In the heads file does it say "filterPageViewController : UIViewController"? If so you are au classing UIViewController. If you are writing your own class you have to subclass (you define what you are sub classing from when you create the class) – jniegsch Jul 29 '16 at 20:20
  • it says, "@interface filterPageViewController : UIViewController " -- so I suppose I am subclassing from UIViewController – maddie Aug 02 '16 at 19:38
  • Nevermind -- I implemented your code incorrectly the first time -- thank you for the help!! It finally works!!! – maddie Aug 02 '16 at 23:52
  • That's great to hear :D – jniegsch Aug 03 '16 at 10:13