2

I have a UITextField (Password Field) that sometimes takes several touches to show the keyboard. This seems to only happen when you tap where the placeholder text is. Here's a shot of the view hierarchy which seems OK to me.

View Hierarchy

Button Position

Any ideas what else to look for so I can figure out why this is happening?


EDIT

So I figured out what the problem is, it's the leftView in the next piece of code:

  UIView *pwPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 38)];
  self.passwordField.leftView = pwPaddingView;
  self.passwordField.leftViewMode = UITextFieldViewModeAlways;

This seems to prevent touches on approximately the first three characters of 'Password'. The reason it's there was to pad the left hand side of the field so the word was not right up against the left margin. What alternatives to I have to either allow touches to be detected or to pad the field in a different way?

conorgriffin
  • 4,282
  • 7
  • 51
  • 88
  • is there anything over it? a label or something..? – DD_ Mar 02 '13 at 16:48
  • Nope there's nothing over it – conorgriffin Mar 02 '13 at 17:30
  • did you add any gesture recognizers? type "po [[UIWindow keyWindow] recursiveDescription]" in the debugger and check the view hierarchy – Martin Ullrich Mar 02 '13 at 17:35
  • I have two ideas for you. First remove all subViews from your view apart from password textField and check how it works than with touching. Second idea is make you password textField first responder in viewDidLoad method. Do this two option and give here conclusions. – edzio27 Mar 02 '13 at 18:42
  • A significant factor here is that it only seems to be an issue when tapping the placeholder text and not the rest of the field. I'll try out some suggestions and update the question – conorgriffin Mar 02 '13 at 20:58
  • See http://stackoverflow.com/questions/10818861/uitextfield-adding-uiview-for-leftview-cant-get-focus You need to set: pwPaddingView.userInteractionEnabled = NO; – RickJansen Aug 13 '13 at 21:38

4 Answers4

1

Did you set it to not cancel other touches in the view?

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
 tap.cancelsTouchesInView = NO; //add this line
 [self.view addGestureRecognizer:tap];
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
1

Create a subclass of UITextField and set password field to be of this class in the storyboard. Inside the new class override the following methods like so

// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
  return CGRectInset( bounds , 10 , 0 );
}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
  return CGRectInset( bounds , 10 , 0 );
}

and remove the following code from the viewController code:

UIView *pwPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 38)];
self.passwordField.leftView = pwPaddingView;
self.passwordField.leftViewMode = UITextFieldViewModeAlways;

This pads the cell but doesn't prevent touch detection

conorgriffin
  • 4,282
  • 7
  • 51
  • 88
1

To stop the left view swallowing the tap events you can set userInteractionEnabled to NO on the view, e.g.

UIView *pwPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 38)];
pwPaddingView.userInteractionEnabled = NO;
self.passwordField.leftView = pwPaddingView;
self.passwordField.leftViewMode = UITextFieldViewModeAlways;
user833755
  • 11
  • 1
0

Calling

[self resignFirstResponder]

in my ViewController with UITextField helps to resolve periodical unresponsives to touches

Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39