Is there a way to prevent the keyboard from dismissing? Is there a way to prevent the keyboard from dismissing? ios ios

Is there a way to prevent the keyboard from dismissing?


There IS a way to do this. Because UIKeyboard subclasses UIWindow, the only thing big enough to get in UIKeyboard's way is another UIWindow.

- (void)viewDidLoad {    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coverKey) name:UIKeyboardDidShowNotification object:nil];    [super viewDidLoad];}- (void)coverKey {    CGRect r = [[UIScreen mainScreen] bounds];    UIWindow *myWindow = [[UIWindow alloc] initWithFrame:CGRectMake(r.size.width - 50 , r.size.height - 50, 50, 50)];    [myWindow setBackgroundColor:[UIColor clearColor]];    [super.view addSubview:myWindow];    [myWindow makeKeyAndVisible];}

This works on iPhone apps. Haven't tried it with iPad. You may need to adjust the size of myWindow. Also, I didn't do any mem management on myWindow. So, consider doing that, too.


I think I've found a good solution.

Add a BOOL as instance variable, let's call it shouldBeginCalledBeforeHand

Then implement the following methods:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    shouldBeginCalledBeforeHand = YES;    return YES;}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    return shouldBeginCalledBeforeHand;}- (void)textFieldDidBeginEditing:(UITextField *)textField{    shouldBeginCalledBeforeHand = NO;}

As well as

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

to prevent the keyboard from disappearing with the return button. The trick is, a focus switch from one textfield to another will trigger a textFieldShouldBeginEditing beforehand. If the dismiss keyboard button is pressed this doesn't happen. The flag is reset after a textfield has gotten focus.


Old not perfect solution

I can only think of a not perfect solution. Listen for the notification UIKeyboardDidHideNotification and make of the textfields first responder again. This will move the keyboard out of sight and back again. You could keep record of which textfield was the last firstResponder by listening for UIKeyboardWillHideNotification and put focus on it in the didHide.

[[NSNotificationCenter defaultCenter] addObserver:self                                          selector:@selector(keyboardDidHide:)                                              name:UIKeyboardDidHideNotification                                           object:nil];...- (void)keyboardDidHide:(id)sender{    [myTextField becomeFirstResponder];}


matomo