Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent the keyboard from dismissing?

I realize that this is the inverse of most posts, but I would like for the keyboard to remain up even if the 'keyboard down' button is pressed.

Specifically, I have a view with two UITextFields. With the following delegate method

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

I am able to keep the keyboard up even if the user presses the Done button on the keyboard or taps anywhere else on the screen EXCEPT for that pesky keyboard down button on the bottom right of the keyboard.

I am using this view like a modal view (though the view is associated with a ViewController that gets pushed in a UINavigationController), so it really works best from a user perspective to keep the keyboard up all of the time. If anyone knows how to achieve this, please let me know! Thanks!

UPDATE Still no solution! When Done is pressed, it triggers textFieldShouldReturn, but when the Dismiss button is pressed, it triggers textFieldDidEndEditing. I cannot block the textField from ending editing or it never goes away. Somehow, I really want to have a method that detects the Dismiss button and ignores it. If you know a way, please enlighten me!

like image 639
PengOne Avatar asked May 15 '11 01:05

PengOne


People also ask

How do you dismiss a keyboard from the tap?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do I dismiss the keyboard in iOS?

The first two are standard to the iOS system: Stop editing the note, and the keyboard will disappear. This requires clicking somewhere away from the text. Swipe your finger downwards beginning just above the keyboard, so that you catch the keyboard as you move.

How do I hide the keyboard in SwiftUI?

You can now write hideKeyboard() from inside any SwiftUI view. Important: If you're using Xcode 12 you need to use RoundedBorderTextFieldStyle() rather than . roundedBorder .


2 Answers

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.

like image 80
Jacob Barnard Avatar answered Sep 25 '22 05:09

Jacob Barnard


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.

like image 40
Nick Weaver Avatar answered Sep 23 '22 05:09

Nick Weaver