0

I am not getting the behavior I expect out of a text field. Previously, I have instantiated textFields via storyboard or Xib. When I click within the textfields bounds it becomes first responder and when I click outside it resigns first responder.

This isn't the case when I create it programmatically within this void method. I tried emulating the behavior by setting textfield to first responder with a uiTextFieldDelegate method but not actually fire when clicking in the textfield.

- (void)createTextField {
    self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(25, 25, 250, 35)];
    self.searchField.delegate = self;
    //[self.searchField becomeFirstResponder];
    [self.mapView addSubview:self.searchField];
    self.searchField.borderStyle = UITextBorderStyleRoundedRect;
    self.searchField.placeholder = @"type here";
    self.searchField.backgroundColor = [UIColor redColor];
    self.searchField.userInteractionEnabled = YES;
    self.searchField.clipsToBounds = YES;
}
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
  • possible duplicate of [Hiding the Keyboard when losing focus on a UITextView](http://stackoverflow.com/questions/1456120/hiding-the-keyboard-when-losing-focus-on-a-uitextview) – Epicblood Jul 28 '15 at 03:43

1 Answers1

1

UITextField does not resign the first responder when tapping outside. You have to do that programmatically like this answer or have another UIResponder become the first responder.

Community
  • 1
  • 1
Antonis
  • 114
  • 7
  • the main problem I am having is that the text field does not become first responder when tapped. – Alexander Bollbach Jul 28 '15 at 11:59
  • i found the bug. It was to do with Google Map view. As per googles instructions I was setting the map view to my view controller's view like: self.view = self.mapView. Somehow, when adding my textfield as a subView either to self.view, or self.mapview (either should be ok?) it was breaking the textfields behavior. – Alexander Bollbach Jul 28 '15 at 12:08