1

I try a very simple example. I add a text view and a button to the view of a view controller. When the button is pressed, it shows an alert view with a text field.

My question is the following:

Suppose I am editing on the text view and press the button to show the alert view. The keyboard will first dismiss (text view resigning first responder) and then appear again (text field becoming first responder). This is really annoying. I want to see if I can do something, so that the keyboard would not dismiss and stay when I switch from the text view to the text field. Thank you all.

Here are some codes of this simple example:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Text view
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
    textView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:textView];

    // Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100,500, 200, 100);
    [button setTitle:@"Show alert view" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)buttonPressed {
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
    [ac addAction:cancelAction];
    [ac addTextFieldWithConfigurationHandler:^(UITextField *textField) {}];
    [self presentViewController:ac animated:YES completion:nil];
}
Mark Lau
  • 11
  • 2

2 Answers2

0

Based on this answer : Keyboard hide and show again...

I think you can just add these lines at the end of your .m file and it should do the job!

// UI AlertController Category
@interface UIAlertController (NonFirstResponder)
@end

@implementation UIAlertController (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
    return NO;
}
@end

// UIAlertAction Category
@interface UIAlertAction (NonFirstResponder)
@end

@implementation UIAlertAction (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
    return NO;
}
@end
Community
  • 1
  • 1
B 7
  • 670
  • 7
  • 23
  • It doesn't work. When the alert view is presented, the keyboard moves down a little bit (trying to dismiss) and then quickly move up back to its original position. – Mark Lau Jul 29 '15 at 13:40
  • This is because of the animation when presenting UIAlertController! You can disable it with `[self presentViewController:ac animated:NO completion:nil];` but I think a quick shadow will appear on your screen. It's important to understand that UIAlertController is a view controller that comes at the top. So, maybe it's easier to code a custom UIView that looks like UIAlertView with UITextField?! Also I don't know what you want to do, but maybe the best UX/UI solution... – B 7 Jul 29 '15 at 15:40
-1

Implement the UITextFieldDelegate Protocol and add this method to your controller:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
      return NO;
 }

This should prevent the keyboard from being dismissed.

Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
  • textFieldShouldReturn is not called. Other delegate methods, such as textFieldDidBeginEditing, are called. So, I believe that the delegate is not nil. – Mark Lau Jul 29 '15 at 13:45