IOS study notes -- View move up and keyboard bounce back

Source: Internet
Author: User

During iOS development, when the keyboard appears, the output port UITextField is overwritten and the output content cannot be seen by the user. In this case, the current view needs to be moved up accordingly. When the output ends, click anywhere on the screen to bring the keyboard back.

The first method is to add a method for moving a view before and after UITextField editing. The second method is to create a new method for moving a view, and determine whether to make the corresponding movement.

To paste the two methods, you must add the UITextFieldDelegate protocol to the. h file and set the delegate.

// *** Change the frame value *** // call the method before UITextField editing-(void) textFieldDidBeginEditing :( UITextField *) textField {// set the Animation name [UIView beginAnimations: @ "Animation" context: nil]; // set the Animation interval [UIView setAnimationDuration: 0.20]; //? Use the currently running status to start the next animation [UIView setAnimationBeginsFromCurrentState: YES]; // you can specify the displacement self of the view movement. view. frame = CGRectMake (self. view. frame. origin. x, self. view. frame. origin. y-100, self. view. frame. size. width, self. view. frame. size. height); // set the animation end [UIView commitAnimations];} // call method after UITextField editing is complete-(void) textFieldDidEndEditing :( UITextField *) textField {// set the Animation name [UIView beginAnimations: @ "Animation" context: nil] ; // Set the animation interval [UIView setAnimationDuration: 0.20]; //? Use the currently running status to start the next animation [UIView setAnimationBeginsFromCurrentState: YES]; // you can specify the displacement self of the view movement. view. frame = CGRectMake (self. view. frame. origin. x, self. view. frame. origin. y + 100, self. view. frame. size. width, self. view. frame. size. height); // sets the animation end [UIView commitAnimations];}

Method 2:

// Call method-(void) textFieldDidBeginEditing (UITextField *) textField {[self animateTextField: textField up: YES] Before UITextField editing;} // complete the call method in UITextField editing-(void) textFieldDidEndEditing :( UITextField *) textField {[self animateTextField: textField up: NO];} // view move up method-(void) animateTextField: (UITextField *) textField up: (BOOL) up {// set the view move up distance, unit: const int movementDistance = 100; // tweak as needed // three views Operation to determine whether to move the view up or int movement = (up? -MovementDistance: movementDistance); // set the Animation name [UIView beginAnimations: @ "Animation" context: nil]; // set the Animation start position [UIView setAnimationBeginsFromCurrentState: YES]; // set the animation interval [UIView setAnimationDuration: 0.20]; // set the displacement self of view movement. view. frame = CGRectOffset (self. view. frame, 0, movement); // sets the animation end [UIView commitAnimations];}
To bring the keyboard back, enter the touch method:

// Click the screen to bring the keyboard back-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {[self. view endEditing: 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.