IOS learning-a solution to the problem that the keyboard pops up to block the input box, ios input box

Source: Internet
Author: User

IOS learning-a solution to the problem that the keyboard pops up to block the input box, ios input box

During mobile development such as iOS and Android, we often encounter many situations that require information input, for example, when logging on, you need to enter the account password, query information, registration or application information, and so on, all of which are input through our keyboard. During iOS development, there are two types of input information: UITextField and UITextView. The former is a single-line Input text box, and the latter is a sliding multi-line Input text box. During the 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 is up. Today, we will focus on the complete response process of text box input and the solution to this occlusion problem.

Complete Response Process entered in one text box

First, we need to control the input and output of UITextField and UITextView. We need to use the corresponding proxy protocol UITextFieldDelegate or UITextViewDelegate. The two have slightly different processes in the control text box, but they are similar. Next, we will learn from the official document about the complete response process of text box input. InUITextFieldDelegate official documentationWe can use some methods in the proxy to call the UITextField on the keyboard to implement interaction with the user. In addition, we can also control the UITextField input process. The entire UITextField input process is divided into the following seven steps (In the following process, changing textfield to textView is the response process of TextView.):

Two-keyboard pop-up and hide 2.1 pop-up keyboard control

As for the pop-up and collapse of the keyboard, we can see from the above response process analysis that the pop-up of the keyboard automatically pops up UITextField and UITextView, so we do not need to control it. If you need control, we know that before the pop-up, the following methods of UITextFieldDelegate or UITextViewDelegate are called to control whether to set the current text box to the first responder (becoming the first responder ), the result of the text box becoming the first responder is that the text content can be input and the keyboard is displayed. Therefore, we can use the return value in this method to determine whether the keyboard is displayed.

// UITextField calls this method-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {// YES is returned. If NO is returned, NO is displayed.} // UITextView calls this method-(BOOL) textViewShouldBeginEditing :( UITextView *) textView {// If YES is returned, the keyboard is displayed. If NO is returned, the keyboard is not displayed}
2.1 hide keyboard control

Through the above analysis, we know that the collapsed keyboard is mainly to cancel the first responder identity of the text box, so we need to control the keyboard to call textFieldShouldEndEditing: method to achieve the goal. There are two methods to collapse the keyboard:

  • First, you can click the "return/finish" button on the keyboard to set the event.
  • Second, you can hide the keyboard from the blank space. This method is common now.

The implementation of these two methods on UITextField and UITextView has been mentioned in my previous articles. If you want to know about them, you can directly stamp them here:Ios learning-hide the keyboard

3. Obtain the content of the text box

In the previous analysis, we know that after the text box ends, a method called is textFieldDidEndEditing:/textViewDidEndEditing: method. This method is convenient for us to process the text box content after the input is complete. If a page contains multiple input text boxes of the same type during development, we can set different tags to identify which input text box is currently used for different processing, example:

- (void)textViewDidEndEditing:(YYTextView *)textView{    if (textView.tag == 400) {        NSString *reason = textView.text;        [self.submitInfoDic setObject:reason forKey:@"reason"];    } else {        NSString *remark = textView.text;        [self.submitInfoDic setObject:remark forKey:@"remark"];    }} 
Four-keyboard pop-up occlusion problem

In the previous analysis, we know that the system will send corresponding notifications when the keyboard pops up and is collapsed, so we can judge the position of the keyboard and the position of the current input text box when we receive the keyboard pop-up. If there is shelter, we will translate the current view upwards, when receiving the keyboard recycle notification, it is moved to the original position. Therefore, there are two main steps:

  • Register keyboard pop-up and closed notification events
    # Pragma mark notification management/*** @ brief notification registration * @ return */-(void) registNotification {// observe keyboard hide and show communications to resize the text view appropriately [[NSNotificationCenter defaultCenter] addObserver: self selector: @ selector (keyboardWillShow :) name: Your object: nil]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];}
  • Events that respond to keyboard pop-up and notification events
    # Pragma mark -- collapse management on the keyboard pop-up-(void) keyboardWillShow :( NSNotification *) note {CGRect frame = self. textViewFrame; // obtain the keyboard height NSDictionary * info = [note userInfo]; CGSize kbSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size; // 140 is the height of the text box. If the height of the text box is different, you can adjust CGFloat offSet = frame. origin. y + 140-(self. view. frame. size. height-kbSize. height); // move the Y coordinate of the attempt to the offset unit up to make the interface free up the space for the display of the soft keyboard if (offSet> 0.01) {WEAKSELF [UIView animateWithDuration: 0.1 animations: ^ {weakSelf. tableView. contentOffset = CGPointMake (0, offSet) ;}}}-(void) keyboardWillHide :( NSNotification *) note {[UIView animateWithDuration: 0.1 animations: ^ {self. tableView. contentOffset = CGPointMake (0, 0) ;}];}

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

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.