Workaround for the "Go" IOS input box being covered by the keyboard

Source: Internet
Author: User

When doing iOS development, you will inevitably encounter the input box is covered by the keyboard problem. On the Internet to search a lot of related solutions, see a lot, but sincerely feel too troublesome.

The solution is to add everything on the view to a scrolling view object (Uiscrollview), and then scroll through the view to make the input box not covered by the soft keyboard, which the individual feels is good, but too troublesome.

Some of the solutions are implemented through a notification uikeyboarddidshownotification, which requires event snooping, and it is necessary to define and implement the methods in the two listener events, "to start editing" and "End edit". I also feel very troublesome.

Many methods are not ideal. Self-study, since the soft keyboard (Keyboard) appears or not is closely associated with the input box (Uitextfield). So you find a solution, without the above two scenarios so troublesome, just implement agent Uitextfielddelegate in the three methods.

Implementation method:

1) Set the proxy for the input box to self

(In the LB file, set the delegate of the input box to file's Owner.) or use code textfield.delegate = self;

2) The Uitextfielddelegate protocol is implemented by the ViewController.h settings corresponding to the input box.

There are three ways to implement Uitextfielddelegate in a viewcontroller.m file:

[CPP]View Plaincopy
  1. When you start editing the input box, the soft keyboard appears, performing this event
  2. -(void) textfielddidbeginediting: (Uitextfield *) TextField
  3. {
  4. CGRect frame = textfield.frame;
  5. int offset = FRAME.ORIGIN.Y +-(self.view.frame.size.height-216.0); //Keyboard height 216
  6. Nstimeinterval animationduration = 0.30f;
  7. [UIView beginanimations:@"Resizeforkeyboard" context:nil];
  8. [UIView setanimationduration:animationduration];
  9. //Move the y-coordinate of the view up to offset units to make room for soft keyboard display
  10. if (offset > 0)
  11. Self.view.frame = CGRectMake (0.0f,-offset, Self.view.frame.size.width, self.view.frame.size.height);
  12. [UIView commitanimations];
  13. }
  14. Keyboard disappears when the user presses return or presses the ENTER key
  15. -(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
  16. {
  17. [TextField Resignfirstresponder];
  18. return YES;
  19. }
  20. Restore the view to its original state when the input box is finished editing
  21. -(void) textfielddidendediting: (Uitextfield *) TextField
  22. {
  23. Self.view.frame =cgrectmake (0, 0, self.view.frame.size.width, self.view.frame.size.height);
  24. }
[CPP]View Plaincopy
  1. When you start editing the input box, the soft keyboard appears, performing this event
  2. -(void) textfielddidbeginediting: (Uitextfield *) TextField
  3. {
  4. CGRect frame = textfield.frame;
  5. int offset = FRAME.ORIGIN.Y +-(self.view.frame.size.height-216.0); //Keyboard height 216
  6. Nstimeinterval animationduration = 0.30f;
  7. [UIView beginanimations:@"Resizeforkeyboard" context:nil];
  8. [UIView setanimationduration:animationduration];
  9. //Move the y-coordinate of the view up to offset units to make room for soft keyboard display
  10. if (offset > 0)
  11. Self.view.frame = CGRectMake (0.0f,-offset, Self.view.frame.size.width, self.view.frame.size.height);
  12. [UIView commitanimations];
  13. }
  14. Keyboard disappears when the user presses return or presses the ENTER key
  15. -(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
  16. {
  17. [TextField Resignfirstresponder];
  18. return YES;
  19. }
  20. Restore the view to its original state when the input box is finished editing
  21. -(void) textfielddidendediting: (Uitextfield *) TextField
  22. {
  23. Self.view.frame =cgrectmake (0, 0, self.view.frame.size.width, self.view.frame.size.height);
  24. }


It's a simple method, isn't it? Please note that you must not forget to set the proxy for the input box delegate OH

The implementation effect is as follows:

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.