How to solve the problem of hiding view on the keyboard-iOS development

Source: Internet
Author: User

Original article Excerpted from:Http://blog.csdn.net/iukey/article/details/7242488

By default, opening the keyboard will cover the view below, which brings a little trouble. However, this is not a big problem. We can solve it with a few small means.

First, we need to know that the keyboard height is fixed, but after IOS 5.0, the keyboard height does not seem to be 216, but it doesn't matter. We just need to adjust it:

 

IPhone

IPad

Portrait screen (portrait)

216

264

Landscape)

140

352

The method we take is to move self. view up 216px as a whole when textfield (possibly another control) receives a pop-up keyboard event (we will take the iPhone portrait screen as an example ).

For details about frame, origin, size, and other aspects of a view, see another blog: <several basic knowledge points about the View>

First, we need to set the textfield proxy to the current controller.

Textfield, delegate = self;

Then we implement the following three delegate methods in the current controller:

[Java]View plaincopyprint?

  1. -(Void) Textfielddidbeginediting :( uitextfield *) textfield
  2. {// This method is called when you click inside textfield to start editing. Textfield will become first responder
  3. Nstimeinterval animationduration = 0.30f;
  4. Cgrect frame = self. View. frame;
  5. Frame. Origin. Y-= 216;
  6. Frame. Size. Size + = 216;
  7. Self. View. Frame = frame;
  8. [Uiview beginanimations: @ "resizeview" context: Nil];
  9. [Uiview setanimationduration: animationduration];
  10. Self. View. Frame = frame;
  11. [Uiview commitanimations];
  12. }

[Java]View plaincopyprint?

  1. -(Bool) textfieldshouldreturn :( uitextfield *) textfield
  2. {// When the user presses ruturn and removes the focus from textfield, the keyboard disappears.
  3. Nstimeinterval animationduration = 0.30f;
  4. Cgrect frame = self. View. frame;
  5. Frame. origins. Y + = 216;
  6. Frame. Size. Height = 216;
  7. Self. View. Frame = frame;
  8. // Self. View
  9. [Uiview beginanimations: @ "resizeview" context: Nil];
  10. [Uiview setanimationduration: animationduration];
  11. Self. View. Frame = frame;
  12. [Uiview commitanimations];
  13. [Textfield resignfirstresponder];
  14. }

 

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.