IOS hides the keyboard and input methods to prevent text boxes from being blocked.

Source: Internet
Author: User

I entered "iOS hidden keyboard" in Baidu and quickly found many articles. For example, "click return to hide", "click other places in the input box to hide", and so on.


In fact, hiding the input method is also simple. We should grasp its essence: Call the resignfirstresponder function to implement hiding (which will be described below) resignfirstresponder

Of course, you can call it anywhere below to hide the input method keyboard.

-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event

{

Nslog (@ "% @", @ "pointer began ");

}

-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event

{

Nslog (@ "% @", @ "pointer move ");

[Textfieldresignfirstresponder]; // hide the input method when sliding

}

-(Void) touchesended :( nsset *) touches withevent :( uievent *) event

{

Nslog (@ "% @", @ "pointer end ");

}

You can also click "return" to hide it automatically. Note: This method must inherit the uitextfielddelegate protocol.

Only uiviewcontroller inherits from uitextfielddelegate. The uiview class does not work if it inherits uitextfielddelegate.

We need to continue our research, so why does uiview fail.

(Ah, that's a big deal: It's because my uiview didn't implement the uitextfielddelegate protocol, plus contentfield. Delegate = self)

The resignfirstresponder submits the identity of the first responder of the Input Method to hide the content. To be continued ..... Prevent text box obstruction

This article from http://xscconan.blog.163.com/blog/static/149593132012111310351998/

The main idea is that when the keyboard pops up and is hidden, the corresponding view also has the effect of moving up or down:

Premise: View implements the protocol of the input box, contentfield. Delegate
= Self;


// When you start to edit the input box, the keyboard appears. Execute this event.

-(Void) textfielddidbeginediting :( uitextfield *) textfield

{

Int offset = 216; // frame. Origin. Y + 43-(self. Frame. Size. Height-216.0); // The keyboard height is 216.

// 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)

{

Nstimeinterval animationduration =
0.30f;

[Uiviewbeginanimations: @ "resizeforkeyboard" context: Nil];

[Uiviewsetanimationduration: animationduration];

Self. Frame = cgrectmake (0.0f,-offset, self. Frame. Size. Width, self. Frame. Size. Height );

[Uiviewcommitanimations];

}

}

// When the input box is hidden, the view moves down accordingly.

-(Void) textfielddidendediting :( uitextfield *) textfield

{

Nstimeinterval animationduration =
0.30f;

[Uiviewbeginanimations: @ "resizeforkeyboard" context: Nil];

[Uiviewsetanimationduration: animationduration];

// 20 is the height of topbar. The size of view. frame is [0,460].

Self. Frame = cgrectmake (0, 20,
Self. Frame. Size. Width, self. Frame. Size. Height );

[Uiviewcommitanimations];

}

Perfect implementation ..


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.