Solve the problem that the keyboard blocks the input box. The keyboard blocks the input box.

Source: Internet
Author: User

Solve the problem that the keyboard blocks the input box. The keyboard blocks the input box.
1) First, follow the protocol UITextFieldDelegate @ interface userInfoViewController () <UITextFieldDelegate> 2) set the proxy (the following self is the parent view of the input box) textField. delegate = self; 3) implement the three methods of UITextFieldDelegate:-(BOOL) textFieldShouldReturn :( UITextField *) textField {
[TextField resignFirstResponder];
Return YES;
}/*** The following is a full-screen view. The view has a sub-control textField. ***/-(void) textFieldDidBeginEditing :( UITextField *) textField {CGRect frame = textField. frame; int offset = frame. origin. y + 32-(self. frame. size. height-256.0); // The keyboard height is 256, and the textField height is 32.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations: @ "ResizeForKeyboard" context: nil];
[UIView setAnimationDuration: animationDuration];

// Move the Y coordinate of the view to the offset Unit so that the following space is used for the display of the soft keyboard.
If (offset> 0)
Self. frame = CGRectMake (0.0f,-offset, self. frame. size. width, self. frame. size. height );
[UIView commitAnimations];}-(void) textFieldDidEndEditing :( UITextField *) textField {self. frame = CGRectMake (0, 0, self. frame. size. width, self. frame. size. height);}/*** a full-screen view with tableView. Each tableViewCell has a sub-control textField. ***/-(void) textFieldDidBeginEditing :( UITextField *) textField
{
CGRect frame = textField. frame;

Int index = (int) (textField. tag-0x1000 );

// 30 is the height of textField, (index-1) is the first few cells, 90 is the y of tableView, that is, the bottom of the input box is from self. view height minus the top of the keyboard from self. view height
Int offset = frame. origin. y + (index-1) * 50 + 30 + 90-(self. view. frame. size. height-256.0); // The keyboard height is 256.

NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations: @ "ResizeForKeyboard" context: nil];
[UIView setAnimationDuration: animationDuration];

// Move the Y coordinate of the view to the offset Unit so that the following space is used for the display of the soft keyboard.
If (offset> 0)
Self. infoList. frame = CGRectMake (10.0f, 90-offset, self. infoList. frame. size. width, self. infoList. frame. size. height );

[UIView commitAnimations];
}

-(Void) textFieldDidEndEditing :( UITextField *) textField
{
Self. infoList. frame = CGRectMake (10, 90, [UIScreen mainScreen]. bounds. size. width-20,300);} effect:

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.