0

I have a textfield on UIScrollview.

When the user taps outside of UITextfield, I use UIScrollViewKeyboardDismissModeInteractive to dismiss the keyboard. But it is not dismissing the keyboard.

What is wrong with my code?

m_scrollProfile = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
m_scrollProfile.backgroundColor = kBGWhiteColor;
m_scrollProfile.userInteractionEnabled = YES;
[self.m_bgImageView addSubview:m_scrollProfile];

m_scrollProfile.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • This question might help you: http://stackoverflow.com/questions/274319/how-do-you-dismiss-the-keyboard-when-editing-a-uitextfield – Brian Jul 17 '15 at 19:49

1 Answers1

0

I encountered the same issue recently. In my header, I added <UITextFieldDelegate> as follows:

@interface YourClass : UIViewController <UITextFieldDelegate>

I added this in my viewDidLoad:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];

and

self.myTextField.delegate = self;

and this method down below:

- (void)dismissKeyboard:(UITapGestureRecognizer *)recognizer {
    NSLog(@"Dismiss keyboard");
    [self.view endEditing:YES];
}

Not sure what else is going on w/ your implementation, but it works in the app I'm currently working on when the user clicks outside the UITextField.

Adrian
  • 16,233
  • 18
  • 112
  • 180