SoFunction
Updated on 2025-04-12

iOS project development keyboard pop-up occlusion input box problem solution

During the development of mobile terminals such as iOS or Android, we often encounter many situations where we need to enter information, such as entering the account and password when logging in, entering the query information when querying, and filling in some information when registering or applying, etc., we enter it through our keyboard. During the development of iOS, there are two types of information that are generally used to enter:UITextField and UITextView, the former is a single-line input text box, and the latter is a sliding multi-line input text box. During this entire development process, we need to control the pop-up and collapse of the keyboard, and obtain the input information at the end of the input. In addition, we also need to ensure that the text box we entered is not blocked when the keyboard pops up. Today, let’s mainly talk about the complete response process of text box input and the solution to the final occlusion problem.

1. Complete response process for text box input

First of all, if we want to control the input and output of UITextField and UITextView, we need to use some methods in the corresponding proxy protocol UITextFieldDelegate or UITextViewDelegate. The corresponding processes of the two in the control text box are slightly different, but they are almost the same. Next, we will learn the complete response process of text box input from the official document. The official documentation of UITextFieldDelegate explains: We can implement the UITextField calling the keyboard through some methods in the proxy, so as to implement the method of interacting with the user. In addition, we can also control the input process of UITextField. The entire input process of UITextField is divided into the following 7 steps (the following process, if textfield is replaced with textView, it is the response process of TextView):

Before becoming the first responder, the textbox calls its proxy textFieldShouldBeginEditing: method to allow or block its first responder and control whether to enter the textbox

Become the first responder, the corresponding event is the system call keyboard (auto-popup), and the system will issue Notification notifications for UIKeyboardWillShowNotification and UIKeyboardDidShowNotification as needed. If other input views in the system are visible at this time, the system will issue Notifications for UIKeyboardWillChangeFrameNotification and UIKeyboardDidChangeFrameNotification

The textFieldDidBeginEditing: method of the system call agent, and a notification of UITextFieldTextDidBeginEditingNotification is issued. At this time, the cursor has been positioned in the text field, and the keyboard has also popped up. You can enter it next.

During the input process, the textField: shouldChangeCharactersInRange:replacementString: method will be called during the input process, and a notification of UITextFieldTextDidChangeNotification will be issued. In addition, when the user clicks the [clear/Clear] key, the textFieldShouldClear: method is called to clear the content, and when the user clicks the [return/complete] key, the textFieldShouldReturn: method is called. Note: UITextViewDelegate does not have a corresponding clear and complete method, so we cannot call the textFieldShouldClear: method and textFieldShouldReturn: method realizes the effects of the [clear/clear] and [return/complete] keys.

When the text box input is about to end and the first responder is about to be logged out, the system will call the textFieldShouldEndEditing: method

The text box will cancel the first responder. The corresponding response time is that the system will retract the keyboard, and when hiding the keyboard, it will issue notifications of UIKeyboardWillHideNotification and UIKeyboardDidHideNotification.

Finally, the system calls the textFieldDidEndEditing: method to end the input and issue a notification of UITextFieldTextDidEndEditingNotification.

2. The keyboard pops up and closes

2.1 Control of the pop-up keyboard

Regarding the issues of pop-up and closing of the keyboard, from the above analysis of the response process, we can see that the pop-up of the keyboard automatically pops up for UITextField and UITextView, so we do not need to control it. If you need to control it, we know that before the pop-up, the following method of UITextFieldDelegate or UITextViewDelegate is called to control whether the current text box is set to become the first responder. The result of the text box becoming the first responder is that the text content can be entered and the keyboard can be popped up. Therefore, we can use the return value in this method to determine whether the keyboard will be popped up.

//UITextField calls this method- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
 // Return to YES to pop up the keyboard. Return to NO, the keyboard will not pop up.}
//UITextView calls this method- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
 // Return to YES to pop up the keyboard. Return to NO, the keyboard will not pop up.}

2.1 Close the keyboard control

Through the previous analysis, we know that by closing the keyboard, we need to cancel the first responder identity of the text box. Therefore, we need to control the keypad to call the textFieldShouldEndEditing: method when closing the keyboard to achieve the goal. Regarding closing the keyboard, we usually have two ways to do it as follows:

First, set it through the click event of the [return/complete] button on our keyboard

The second is to set the keyboard to close the blank space. This method is now more common.

The implementation of these two methods on UITextField and UITextView has been specifically discussed in my previous essays. Those who want to know can click here directly:ios learning - keyboard closed

Three: Getting the content of the text box

In the previous analysis, we knew that after the text box ends, one method will be called, which is the textFieldDidEndEditing: / textViewDidEndEditing: method. This method is to facilitate us to process the text box content after the input is finished. If a page has multiple input text boxes of the same type during development, we can set different tags to distinguish which input text box is currently in order to perform different processing. The specific examples are as follows:

- (void)textViewDidEndEditing:(YYTextView *)textView{
 if ( == 400) {
  NSString *reason = ;
  [ setObject:reason forKey:@"reason"];
 } else {
  NSString *remark = ;
  [ setObject:remark forKey:@"remark"];
 }
}

4. Problem of occlusion when the keyboard pops up

In the previous analysis, we knew that when the keyboard pops up and closes, the system will send corresponding notifications, so we can judge the keyboard position and the current input text box when we receive the keyboard pops up. If there is an obstruction, we will pan the current view upwards and pan to the original position when we receive the notification collected by the keyboard. Therefore, it is mainly divided into 2 steps:

Register for notification events on keyboard pop-up and collapse

#pragma mark notification management/**
  * @brief Notify registration
  * @return
  */
- (void)registNotification
{
 // observe keyboard hide and show notifications to resize the text view appropriately
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

Implement response events when receiving keyboard pop-ups and closing notification events

#pragma mark --Keyboard pop-up and close management-(void)keyboardWillShow:(NSNotification *)note{
 CGRect frame = ;
 //Get the keyboard height NSDictionary* info = [note userInfo];
 CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
 //140 is the height of the text box. If your text box height is different, you can make different adjustments. CGFloat offSet =  + 140 - ( - );
 //Move the Y coordinate of the attempt upwards offset units to free up the interface for displaying the soft keyboard if (offSet > 0.01) {
  WEAKSELF
  [UIView animateWithDuration:0.1 animations:^{
    = CGPointMake(0, offSet);
  }];
 }
}
-(void)keyboardWillHide:(NSNotification *)note{
 [UIView animateWithDuration:0.1 animations:^{
   = CGPointMake(0, 0);
 }];
}

Many times, we have multiple input text boxes. In our example, we have two input text boxes. How do we determine which text box it is when we receive the notification? In the previous analysis, we know that before issuing a notification, the system will call the textFieldShouldBeginEditing: method of the input text box agent to determine whether editing is allowed. Then we can judge which text box is and the specific position of the text box, etc. in this method, and then determine whether to pan and offset when the keyboard pops up.

- (BOOL)textViewShouldBeginEditing:(YYTextView *)textView{
 //Get the position of the current input text box relative to the current view  = [textView convertRect: toView:];
 return YES;
}