Handling of iOS keyboard overwrite input boxes

Source: Internet
Author: User

On a multi-input interface, multiple UITextfield input boxes are displayed. In terms of scrolling, we will add them to the cell of UITableView one by one to form a data input interface that can slide up and down.

However, the character input is completed through a soft keyboard automatically popped up. Therefore, when the cell at the bottom of the screen is selected, it will be overwritten by the soft keyboard area.

Similarly, because the input box is in the cell of UITableView, you can scroll the cell to the position above the area covered by the soft keyboard. So that we can see the input box.

Now, we need to implement this operation process.

We can simply scroll the cell to the top of the visible area of the table view.

UITableView provides this method "scrollToRowAtIndexPath: atScrollPosition: animated :". As the input box of the first responder, you can use its delegate method-(void) textFieldDidBeginEditing :( UITextField *) textField

Assign it to the lastEditTextField variable. The appearance and disappearance of the soft keyboard are registered in the View Controller class and implemented in this way. In this way, when the UITextField activates the pop-up keyboard, the system will automatically submit a notification for the soft keyboard pop-up. The keyboard pop-up notification calls a method.

The logic implemented by this method is to obtain the operated cell based on the location of lastEditTextField, and then scroll the cell to the top of the visible area of the view.

CGPoint pt = lastEditTextField. center;

Pt = [self. tableView convertPoint: ptfromView: lastEditTextField. superview];

NSIndexPath * indexPath = [self. tableViewindexPathForRowAtPoint: pt];

[Self. tableView scrollToRowAtIndexPath: indexPathatScrollPosition: UITableViewRowAnimationTop animated: YES];

In this Code, the most important thing is according to lastEditTextField. center value, which is converted to self by using the instance method of UIView-(CGPoint) convertPoint :( CGPoint) point fromView :( UIView *) view. the value in the coordinate system of tableview. Then, based on the value of lastEditTextField. center in the new coordinate system, use the UITableView instance method scrollToRowAtIndexPath: atScrollPosition: animated: to scroll the cell to the top of the screen.

When the keyboard disappears, the system will send another notification to restore the screen to its original position.

Scroll the input box to the top, although it can meet the visible requirements of our input. However, if you can scroll to the top of the keyboard, rather than the top of the UITableView, the input box does not need to scroll when it is not covered by the keyboard. Such processing will be more satisfactory.

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.