"go" two features commonly used on iOS: tap the screen and return to exit the hidden keyboard and solve the virtual keyboard blocking Uitextfield method

Source: Internet
Author: User
Tags exit in
<span id="Label3"></p><p><p>iOS above the keyboard processing is very impersonal, so these functions need to be implemented by themselves,</p></p><p><p>first, Click Return and the Screen-hide keyboard</p></p><p><p>The first to cite the Gemini blog http://my.oschina.net/plumsoft/blog/42545, His article is very good, for everyone's understanding is very good.</p></p><p><p></p></p><p><p>In an IOS program, tapping the text box opens the keyboard when you want to enter data in the text box. For the IPad program, the keyboard has a button that can be used to turn off the keyboard, but the keyboard in the IPhone program does not have such a button, but we can take some steps to close it. For example, we can implement pressing the rerun (sometimes done, and Then) keys to turn off the keyboard, or, more humanized, touch the background to turn off the Keyboard.</p></p><p><p>1. first, Press the return key to close the Keyboard.</p></p><p><p>When you press the Return key of the keyboard, a do End on Exit event is generated, and when we tell the text box to discard the control, the keyboard disappears.</p></p><p><p>Let's say We've created a single View application, opened the Viewcontroller.xib file, dragged up the three text Field on the view, and then we mapped the three text boxes to Viewcontroller. h, the names are firstfield, secondfield, and Thirdfield in Turn. Such as:</p></p><p><p></p></p><p><p>On this basis, the realization of the light touch Return to close the keyboard, the steps are:</p></p><p><p>(1) declare a method in ViewController.h:</p></p><pre name="code"><pre name="code">-(ibaction) textfiledreturnediting: (id) sender;</pre></pre><p><p></p></p><p><p>(2) implement this method in Viewcontroller.m:</p></p><pre name="code"><pre name="code">-(ibaction) textfiledreturnediting: (id) Sender { [sender resignfirstresponder];}</pre></pre><p><p></p></p><p><p>The so-called first Responder refers to the control that the user is currently interacting With. When the user uses the keyboard, first Responder is the keyboard, resignfirstresponder method, as the name implies, is to give up first Responder.</p></p><p><p>(3) let the three text boxes map to the textfiledreturnediting method, but the event should be did End on Exit, as Follows:</p></p><p><p>Open Assistant Editor, Open viewcontroller.xib on the left, open ViewController.h on the right, open Connector Inspector on the far right of Xcode, and select the first text in View box, find did End on Exit in Connector Inspector, pull the map line from the circle to its right, and map to the Textfiledreturnediting method of ViewController.h, such as:</p></p><p><p></p></p><p><p>Do the same for the other two text boxes. now, the Touch Return key has been implemented to turn off the Keyboard.</p></p><p><p>2, The following introduction of a more humane approach, touch the background to close the Keyboard.</p></p><p><p>Similar to the above steps, first define a method, then implement this method, then map the specified control to this method and select the event that is Triggered. The difference is that the control we want to select is not the top text box, but the view itself.</p></p><p><p>(1) Add the method declaration code in the ViewController.h file:</p></p><pre name="code"><pre name="code">-(ibaction) backgroundtap: (id) sender;</pre></pre><p><p></p></p><p><p>(2) implement this method in Viewcontroller.m:</p></p><pre name="code"><pre name="code">-(ibaction) backgroundtap: (id) Sender { [firstfield resignfirstresponder]; [secondfield resignfirstresponder]; [thirdfield resignfirstresponder];}</pre></pre><p><p></p></p><p><p>It should be noted that [firstfield resignfirstresponder], if Firstfield have firstresponder to abandon it, we do not have to first determine whether there is firstfield, this statement is completely correct.</p></p><p><p>(3) let the view map to this method, but in advance, we need to change the view type First.</p></p><p><p>Open xib, Select View, Open Identity Inspector, select Uicontrol in Class:</p></p><p><p></p></p><p><p>(4) Open Assistant Editor, open viewcontroller.xib on the left, open ViewController.h on the right, open Connector Inspector on the right side of xcode, Viewcontroller Select Control in The. xib, find Touch down in the Connector Inspector, pull the map line from the circle on its right, and map to the Backgroundtap method of ViewController.h, such as:</p></p><p><p></p></p><p><p>well, you can run it down to see the effect:</p></p><p><p></p></p><p><p></p></p><p><p>After you open the keyboard, click in the background area and the keyboard will pick up.</p></p><p><p></p></p><p><p>then, on the internet, there is only one Backgroundtap function that is written, and then all components are resignfirstresponser, and the events of the component and the events of the screen are directed to the same Function.</p></p><p><p>Both of these methods can be used, but, I prefer to use the same function method, reason, The reason is involved in the second aspect of Knowledge:</p></p><p><p>How to solve the virtual keyboard blocking Uitextfield</p></p><p><p>Because the screen is too small, a keyboard jumps out to always block the input box, so you need to move the screen to match the keyboard</p></p><p><p></p></p><p><p></p></p><p><p>#pragma mark-</p></p><p><p>#pragma mark to solve the virtual keyboard blocking Uitextfield method</p></p><p><p>-(void) keyboardwillshow: (nsnotification *) Noti</p></p><p><p>{</p></p><p><p>Interface Adjustment for keyboard input</p></p><p><p>The height of the keyboard</p></p><p><p>float height = 216.0;</p></p><p><p>CGRect frame = self.view.frame;</p></p><p><p>Frame.size = Cgsizemake (frame.size.width, frame.size.height-height);</p></p><p><p>[UIView beginanimations:@ "Curl" Context:nil];//animation starts</p></p><p><p>[UIView setanimationduration:0.30];</p></p><p><p>[UIView setanimationdelegate:self];</p></p><p><p>[self.view setframe:frame];</p></p><p><p>[UIView commitanimations];</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldreturn: (uitextfield *) TextField</p></p><p><p>{</p></p><p><p>When the user presses return, take focus away from the text field so, the keyboard is Dismissed.</p></p><p><p>Nstimeinterval animationduration = 0.30f;</p></p><p><p>[UIView beginanimations:@ "resizeforkeyboard" context:nil];</p></p><p><p>[UIView setanimationduration:animationduration];</p></p><p><p>CGRect rect = CGRectMake (0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);</p></p><p><p>CGRect rect = CGRectMake (0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);</p></p><p><p>Self.view.frame = rect;</p></p><p><p>[UIView commitanimations];</p></p><p><p>[textField resignfirstresponder];</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) textfielddidbeginediting: (uitextfield *) TextField</p></p><p><p>{</p></p><p><p>CGRect frame = textfield.frame;</p></p><p><p>int offset = FRAME.ORIGIN.Y +-(self.view.frame.size.height-216.0);//keyboard Height 216</p></p><p><p>Nstimeinterval animationduration = 0.30f;</p></p><p><p>[UIView beginanimations:@ "resizeforkeyboard" context:nil];</p></p><p><p>[UIView setanimationduration:animationduration];</p></p><p><p>Float width = self.view.frame.size.width;</p></p><p><p>float height = self.view.frame.size.height;</p></p><p><p>If (offset > 0)</p></p><p><p>{</p></p><p><p>CGRect rect = CGRectMake (0.0f,-offset,width,height);</p></p><p><p>Self.view.frame = rect;</p></p><p><p>}</p></p><p><p>[UIView commitanimations];</p></p><p><p>}</p></p><p><p>#pragma mark-</p></p>As long as the code to add the three files, and then the delegate itself as the upper right corner of the screen can be implemented to move, but there is often a screen after the movement can not return the problem, the solution here is<p><p>-(ibaction) backgroundtap: (id) Sender {</p></p><p><p>Nstimeinterval animationduration = 0.30f;</p></p><p><p>[UIView beginanimations:@ "resizeforkeyboard" context:nil];</p></p><p><p>[UIView setanimationduration:animationduration];</p></p><p><p>CGRect rect = CGRectMake (0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);</p></p><p><p>Self.view.frame = rect;</p></p>Add the code to the BACKGROUNDTAP function so that the screen returns to Normal. from:http://blog.csdn.net/xiaotanyu13/article/details/7711954<p><p>"go" two features commonly used on iOS: tap the screen and return to exit the hidden keyboard and solve the virtual keyboard blocking Uitextfield method</p></p></span>

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.