iOS development - 5 ways to close the keyboard and exit the keyboard
1. Click on a place outside the editing area (UIView)
2. Click on the place outside the editing area (UIControl)
3. Use buttons to create the keyboard
4. Use judgment input characters
5. Questions about keyboard occlusion
1. Click on the place outside the editing area (UIView)
This is a very intuitive method. When you no longer need to use a virtual keyboard, just click on the virtual keyboard and outside the editing area to put the keyboard away. The following code is a touch event method function built in UIView. You can refer to the basic usage of Touch Panel / Touch Screen / Pressure Sensor to find more methods functions about touch events.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (![myTextView isExclusiveTouch]) { [myTextView resignFirstResponder]; } }
If you want to use this method, please remember that the Custom Class on your operation screen must be UIView.
The Custom Class of the screen is UIView
2. Click on the place outside the edit area (UIControl)
The way to close the virtual keyboard is the same as the previous one, but if your touch event is already full of code, you can consider using the Touch Up Inside event of UIControl to close the keyboard by connecting the following code with the Touch Up Inside event of UIControl.
- (IBAction)dismissKeyboard:(id)sender { [myTextView resignFirstResponder]; }
If you want to use this method, please remember that the Custom Class on your operation screen must be UIControl.
The Custom Class of the screen is UIControl
Link the method to close the keyboard with the UIControl event
3. Use the button to create the keyboard to close
When there is no place outside the editing area for clicking to close the keyboard, it is also a good way to create a button to close the current virtual keyboard. Since the button must appear on the virtual keyboard to be displayed on the screen, you must borrow NSNotificationCenter to help us judge the current keyboard status.
First, register with the NSNotificationCenter in the viewDidLoad: event and tell the NSNotificationCenter our doneButtonshow: method function.
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil]; }
Now, whenever the virtual keyboard appears, we will automatically call the customized doneButtonshow: method function. Next, just define the method of the button appearing in the method function.
-(void) doneButtonshow: (NSNotification *)notification { doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; = CGRectMake(0, 228, 70, 35); [doneButton setTitle:@"Complete Editing" forState: UIControlStateNormal]; [doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside]; [ addSubview:doneButton]; }
Finally, it is the hideKeyboard: method function when the button is pressed. Be sure to remove the button in the function.
-(void) hideKeyboard { [doneButton removeFromSuperview]; [myTextView resignFirstResponder]; }
4. Use judgment input characters
If you want to close the keyboard by entering specific characters (such as return new characters), you must first use the agreement in the @interface section within the category. You can refer to the article How to use the Protocol Agreement for more information about the agreement.
After adopting the contract, then implement the textView: shouldChangeTextInRange:replacementText: method function within the contract. This method function will be triggered when the character is entered, and the returned BOOL value represents whether the character needs to be used. The following program code is used in this method function to close the virtual keyboard (judging that the character is a return line break character).
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [myTextView resignFirstResponder]; return NO; } return YES; }
Finally, don't forget to point the proxy object of UITextView to yourself in the viewDidLoad: event, so that the program can correctly find the category that implements the contract method function.
- (void)viewDidLoad { [super viewDidLoad]; = self; }
5. Questions about keyboard occlusion
If you have encountered the problem of keyboard blocking editing areas in your implementation, you can refer to the article "Animation to solve the problem of keyboard blocking UITextField". Through the Animation function of Core Graphic, you can move the editing areas at the same time when the keyboard appears to solve the problem of occlusion.
The above is the 5 ways to sort out the information for IOS: Turn off the keyboard and exit the keyboard. Thank you for your support for this site!